Object-oriented Javascript-Interface

Source: Internet
Author: User
This article demonstrates the use of Javascript object-oriented interfaces. I hope it will be helpful to you. Let's take a look at the interface below: some of the principles specified for implementing a task, function, and purpose.

Interface in the program: specify several methods and the method name. (Because the tasks to be completed in the program are implemented through functions or methods .)

Interface in javascript: compare an "type object" of the instance with the "interface object" of the instance to conform to the rules. You can say: this object implements the specified interface;

(Interface Class: this class is used to implement different interfaces. That is, different interface instances, that is, the number and name of different methods)

(For comparison: the essence is to determine whether the object of the Child-type instance has the method name stored in the interface object and the number of methods .)

Small instance:

The following sub-categories are available: "server", "Mobile Phone", and "tablet phone. These sub-classes share a common task, function, or purpose-[disconnect the phone]

To implement this function, different sub-classes can have different internal implementation methods, so that the phone can be called. However, for the user's consideration, a rule must be made:

No matter what type you are, the objects that come out of your instance, that is, the objects that have the phone function, there must be two methods to achieve [disconnect the phone,

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

The following is a fixed design pattern:

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
 
  

The following is an example

// Through the interface class, the instance generates an "Disconnect call" interface. Now this interface object is testInterface, which specifies two methods, and the method names are "callfun" and "callnum" var testInterface = new Interface ("call", ["callfun", "callnum"]); // mobile phone class, var mobilepone = function (call) {this. name = call;} // public method of the mobile phone class mobilepone. prototype = {"constructor": mobilepone, // it must be the same as the method name specified in the preceding interface object; "callfun": function () {document. write ("Press") ;}, // it must be the same as the method name specified in the previous interface object "callnum": function () {document. write ("dial") ;}}// a Samsung mobile phone object var anycall = new mobilepone ("anycall") in the mobile phone category "); // check whether the Samsung mobile phone object implements the [disconnect phone] Interface. That is, the Samsung mobile phone object and the Interface object are passed into the verification function as parameters to compare interfaces. ensureImplements (anycall, testInterface); anycall. callnum ();


For more Javascript object-oriented-interface-related articles, refer to the PHP Chinese website!

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.