Home > Web Front-end > JS Tutorial > Javascript object-oriented (3) interface code_js object-oriented

Javascript object-oriented (3) interface code_js object-oriented

WBOY
Release: 2016-05-16 17:53:21
Original
1066 people have browsed it

Interface in the program: Specify how many methods there are and what are the method names. (Because the tasks to be completed in the program are all implemented through functions or methods.)

Interface in JavaScript: a certain "type object" that comes out of the instance, and an "interface object" that comes out of the instance, By making a comparison and complying with the rules, you can say: This object implements the specified interface;

(Interface class: Through this class, different interfaces are instantiated. That is, different interface instances, that is, different Number of methods and method names)
(Comparison: the essence is to determine whether the object from the subtype instance has the method name saved in the interface object, and the number.)
Small example:
Telephone category, under which there can be subcategories such as "phone", "mobile phone", "tablet phone", etc. And these subclasses all have a common task, function or purpose---[Dial the phone]
In order to realize this function, different subclasses can have different internal implementation methods so that the phone can Pull through. But now for the sake of users, a provision must be made:
No matter what subtype you are, the object you instantiate, that is, the object with the phone function, must have two methods to implement [make a call],
That is: 1. Press the phone number (numeric key) 2. Press the dial key;
The following is the fixed design pattern:

Copy code The code is as follows:

var Interface = function(name,methods){
if(arguments.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,len = methods.length; i if(typeof methods[i] !== 'string'){
throw new Error("The name of the interface method must be a String");
}
this.methods.push(methods[i]);
}
};
//Static class Method
Interface.ensureImplements = function( myobject1,Iobject1){
if(arguments.length!=2){
throw new Error("Method Interface.ensureImplemnents specified " arguments.length " parameters, but expected 2.");
}
for(var i=1,len = arguments.length; ivar _interface = arguments[i];
//Interface object, through interface class The instance comes out
if(_interface.constructor !== Interface){
throw new Error("The interface is not created through the Interface class, the instance comes out");
}
//Put the interface object inside Take out the method name and combine it with the mobile phone object in this example to verify whether the mobile phone object has these two methods and whether the method names are the same;
for(var j=0, methodsLen = _interface.methods. length; jvar method = _interface.methods[j];
if(!myobject1[method]||typeof myobject1[method] !== 'function'){
throw new Error("Pass verification function: Interface.ensureImplements: " myobject1.name "Object method " method " cannot be found or is not a Function");
}
}
}
} ;

The following is an example
Copy the code The code is as follows:

//Through the interface class, instantiate an interface for "making a call"; now this interface object testInterface specifies two methods, and the method names are "callfun" and "callnum"
var testInterface = new Interface( "call",["callfun","callnum"]);
//Constructor of mobile phone class;
var mobilepone = function(call){
this.name = call;
}
//Public methods of the mobile phone class
mobilepone.prototype = {
"constructor":mobilepone,
//Must be the same as the method name specified by the previous interface object;
" callfun" : function(){
document.write("key");
},
//Must be the same as the method name specified in the previous interface object
"callnum" : function(){
document.write("Dial");
}
}
//Instance a Samsung mobile phone object through the mobile phone class
var anycall = new mobilepone("anycall") ;
//Check whether this Samsung mobile phone object implements the [Dial a call] interface; that is, pass the Samsung mobile phone object and the interface object as parameters into the verification function for comparison
Interface.ensureImplements( anycall,testInterface);
anycall.callnum();
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template