Three ways to implement the JavaScript interface of recommends

  • 2021-06-28 10:41:19
  • OfStack

Javascript can imitate interfaces in three ways: 1. Annotation 2. Check Attribute 3. Duck Shape Identification

1. Annotation method: This method belongs to the scope of program documentation, and the inheritance of interfaces is completely dependent on the programmer's awareness.


/*
interface People{
function createHead();
function createBody();
}
*/
var woman = function(name){ //implements People interface
this.name = name;
}
woman.prototype.showName = function(){
alert(this.name);
}
woman.prototype.createBody = function(){ // Implement the necessary methods 
alert(" Your body is created ");
}
woman.prototype.createHead = function(){
alert(" Header created ");
} 

//2. Attribute checking: Add interface methods implemented to the list of class attributes, and check repeatedly whether those methods have been implemented through well-defined checks
//Advantages and disadvantages: Programmers can be forced to implement interfaces without error.However, although you have declared which methods you have implemented, there are likely to be omissions in your implementation


/*
interface People{
function createHead();
function createBody();
}
*/
var woman = function(name){
this.name = name;
this.implementsInterfaces = ['People'];
}
woman.prototype.showName = function(){
alert(this.name);
}
woman.prototype.createBody = function(){ // Implement the necessary methods 
alert(" Your body is created ");
}
woman.prototype.createHead = function(){
alert(" Header created ");
}
function implement(obj,interfaces){
for(var i=1;i<interfaces.length;i++){
var interfaceName = interfaces[i];
var interfaceFound = false;
for(var j=0;j<obj.implementsInterfaces.length;j++){
if(obj.implementsInterfaces[j] = interfaceName){
interfaceFound = true;
break;
}
}
if(!interfaceFound){
return false;
}
}
return true;
}
function isImplememts(instance,interfaces){ // Determine whether the object has inherited the appropriate interface 
if(!implement(instance,interfaces)){
throw new Error("Object doesn't implement a required interface");
}
} 

3. Duck Style Identification: (Duck is not judged by its appearance, but by whether it has the characteristics of a duck or not.As James Whitcomb Riley said, it is a duck that walks like a duck and quacks)

Both of the above declare that they have implemented those interfaces. In fact, the declaration is not important. At the core of the implementation of the interface is that the class implements the interface method set.If a class has a function with the same name for all the method functions defined by the interface, it is assumed to implement the interface


// Interface class to create an interface 
var Interface = function(name,motheds){
if(agruments.length!=2){
throw new Error("Interface constructor called with "+arguments.length+"arguments,but expected exactly 2");
}
this.name = name;
this.methods = [];
for(var i=0;i<motheds.length;i++){
if(typeof motheds[i] !== 'string'){
throw new Error('Interface constructor expects mothed names to be'+'passes in as a string');
}
this.methods.push(motheds[i]);
}
}
Interface.prototype.ensureImplements = function(objs){
if(agruments.length != 1){
throw new Error("Interface constructor called with "+arguments.length+"arguments,but expected exactly 1")
}
for(var i=0;i<objs.length;i++){
var obj = objs[i];
for(var j=0;j<this.motheds.length;j++){
var mothed = this.methods[j];
if(!obj[mothed] || !typeof obj[mothed] !== 'function'){
throw new Error('Function Interface.ensureImplements:implements interface'+this.name+',obj.mothed'+mothed+'was not found');
}
}
}
}
// Create Interface 
var People = new Interface('People',['createHead','createBody']);
// Subclass 
var Woman = function(name){
this.name = name;
this.implementsInterfaces = ['People'];
}
Woman.prototype.showName = function(){
alert(this.name);
}
Woman.prototype.createBody = function(){ // Implement the necessary methods 
alert(" A woman's body has been created ");
}
Woman.prototype.createHead = function(){
alert(" A woman's head has been created ");
}
// Subclass 
var Man = function(name){
this.name = name;
this.implementsInterfaces = ['People'];
}
Man.prototype.showName = function(){
alert(this.name);
}
Man.prototype.createBody = function(){ // Implement the necessary methods 
alert(" Men's bodies have been created ");
}
Man.prototype.createHead = function(){
alert(" Men's head has been created ");
}
// Determine whether it is implemented 
Poeple.ensureImplements(['Woman','Man']);


Related articles: