The object-oriented-interface _javascript techniques of JavaScript

Source: Internet
Author: User
Tags static class

Interface: A set of principles for accomplishing a task, function, purpose, etc.

Interfaces in programs: There are several ways to specify what the method name is. (Because the program to complete the task, is through the function or the method to achieve.) )

JavaScript interface: the instance out of a "type Object", and an instance of the "interface object", a comparison, in line with the rules, it can be said: This object to achieve the specified interface;

(Interface class: This class is used to instantiate a different interface.) Different interface instances, that is, the number of different methods and the name of the method

(Comparison: The essence is to judge the object of the subtype instance, whether there is a method name stored in the interface object, and number.) )

Small example:

Phone class, the following can be "sitting machine", "mobile", "tablet computer phone" and so on these subclasses. And these subclasses have a common task, function, or purpose---[unplug the phone]

In order to achieve this function, different subclasses can have different internal implementation methods, so that the phone can be unplugged. But now for the user to consider, it is necessary to make a provision:

No matter what subtype you are, the object you've instantiated, that is, the object that has the telephone function, there must be two methods to implement the [dial phone].

That is: 1. Press the phone number (number key) 2. Press the dialing key;

The following is a fixed design pattern:

var Interface = function (name,methods) {if (Arguments.length!= 2) {throw new Error ("Interface constructor called wit 
 H "+ arguments.length +" arguments, but expected exactly 2. "); 
 } this.name = name; 
 This.methods = []; for (var i = 0,len = Methods.length I <len; i++) {if (typeof Methods[i]!== ' string ') {throw new Error ("The name of the interface method must be 
  is 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 Specifies a" + arguments.length+ "parameter, but expects 2."); 
  for (var i=1,len = arguments.length; i<len; i++) {var _interface = arguments[i]; 
  Interface object, through the interface class instance to come out if (_interface.constructor!== interface) {throw new Error ("interface, not through the interface class, the instance comes out"); ///Take out the method name in the interface object and combine it with the mobile object in this example to verify that the phone object has these two methods, and the method name is not the same; for (var j=0, methodslen = _interface.methods.length; j&lt ; methodslen;j++) {var method = _interface.methods[j]; if (!myobject1[method]| | typeof Myobject1[method]!== ' function ') {throw new Error ("Validation function: interface.ensureimplements: +myobject1.name+" Object method 
   "+ method +" Cannot find or is not a function "); } 
  } 
 } 
};

Here is an example

Through the interface class, an example of a "dial the phone" interface; Now this interface object is TestInterface, which provides two methods, and the method name is "Callfun" and "Callnum"
var testinterface = new Interface ("Call", ["Callfun", "Callnum"]);
Mobile phone class, constructor,
var mobilepone = function (call) { 
 this.name = call;
}
Public method of mobile phone class
Mobilepone.prototype = {
 "constructor": Mobilepone,
 //must be the same as the method name specified in the preceding interface object;
 "Callfun": function () {
  document.write ("key");
 },
 //Must be as "Callnum" as the method name specified in the preceding interface object
 : function () {
  document.write ("Dial number");
 }
Via mobile phone class, instance of a Samsung mobile Object
var anycall = new Mobilepone ("Anycall");
Detection of the Samsung mobile phone object, whether the implementation of the [dial phone] This interface, that is, Samsung mobile phone objects and interface objects as parameters in the validation function, compare
interface.ensureimplements (Anycall, TestInterface);
Anycall.callnum ();

The above is the entire content of this article, I hope to help you, interested can see the "JavaScript Object-oriented-method" and "JavaScript object-oriented-encapsulation," Thank you to the cloud-Habitat community support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.