There are three ways to imitate the Javascript interface: 1. Comment method 2. Check attribute method 3. Duck-style shape identification method
1. Comment method: This method belongs to the category of program documents, and the inheritance and implementation of interfaces is completely consciously dependent on programmers.
/*interface People{function createHead();function createBody();}*/var woman = function(name){ //implements People interfacethis.name = name;}woman.prototype.showName = function(){alert(this.name);}woman.prototype.createBody = function(){ //Implement the necessary method alert("The body has been created");}woman.prototype.createHead = function(){alert("The head has been created");}//2. Attribute checking method: add the interface method to be implemented to the class attribute list, and repeatedly check whether those methods have been implemented through defined detection.
//Pros and cons: You can force programmers to implement the interface, and if you don’t realize it, you will report an error. However, although it declares what methods you have implemented, there are likely to be omissions during 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("The body has been created");}woman.prototype.createHead = function(){alert("The head has been 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){ //Judge whether the object has inherited the corresponding interface if(!implement(instance,interfaces)){throw new Error("Object doesn't implement a required interface");}}3. Duck-style identification method: ( Duck is not judged by appearance, but by whether it has the characteristics of a duck. As James Whitcomb Riley said, the one who walks like a duck and quarrels is the duck)
Both of the above declare that they have implemented the interfaces, but the statement is not important. The core of the implementation of the interface is that the class implements the interface method set. If the class has functions with the same method function name as the interface defined, it is considered to implement the interface
//Interface class, used to create 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 mothereds[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 method alert("The woman's body has been created");}Woman.prototype.createHead = function(){alert("The 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 method alert("The man's body has been created");}Man.prototype.createHead = function(){alert("The man's head has been created");}//Defend whether to implement Poeple.ensureImplements(['Woman','Man']);