01. Interface in Javascript

Source: Internet
Author: User

 

01. Interface ---- interface in Javascript

This article describes how to use native JavaScript code to simulate and implement interfaces.

Preface

As we all know, in Java, C #, and other languages, interfaces are defined by special keyword interfaces, while the implementation of interfaces is completed by the implements keyword. What are the characteristics of interfaces? To put it simply, there are (incomplete induction ):

  1. Cannot be instantiated
  2. All methods are abstract methods.
  3. All attributes are public static final

However, in Javascript, there are no ready-made keywords that can be directly used to define an interface, and no ready-made keywords can be directly used to implement the so-called extends and implements. Since there is no ready-made interface, the following describes how to use native JavaScript code to simulate and implement interfaces.

Interface interface definition in Javascript
/*** Interface constructor * @ Param {string} Name Interface Name * @ Param {array} abstract method in the methods interface * @ exception {error} thrown when the parameter is unqualified * @ example * var iactionlistener = new interface ("iactionlistener ", ["Method1", "method2"]); */var interface = function (name, methods) {If (arguments. length! = 2) {Throw new error ("interface has only two parameters, and the current number of parameters is:" + arguments. length);} This. name = Name; this. methods = []; for (VAR I = 0, Len = methods. length; I <Len; I ++) {If (typeof methods [I]! = 'String') {Throw new error ("the method name in interface must be legal");} This. Methods. Push (methods [I]) ;}};

This interface class is used to simulate the interface keyword in Java, but the difference is that
Define an interface in the form of iactionlistener {}. In this example, VAR iactionlistener = new
Interface ("iactionlistener", ["Method1.

This interface class accepts two parameters. The first parameter indicates the interface name to be defined, and the second parameter indicates the abstract method to be declared in this interface. When this class generates a new interface, it checks the validity of the parameter. Once it passes the check, the interface is generated and has two attributes: Interface Name and method list.

Interface implementation
/*** Interface implementation check, ensure that the object is an interfacen instance * @ Param {object} ointerface instance to be checked * @ Param {class} interfacen is thrown when the interface * @ exception {error} parameter implemented by interfacen is invalid exception * @ example * interface. ensureimplements (oactionlistener, iactionlistener); */interface. ensureimplements = function (ointerface, interface1, interface2, interfacen) {If (arguments. length <2) {Throw new error ("interface. the ensureimplements method requires at least two parameters, and the current number of parameters is: "+ Arguments. length);} For (VAR I = 1, Len = arguments. length; I <Len; I ++) {var interface = arguments [I]; If (interface. constructor! = Interface) {Throw new error (interface + "is not an interface instance, not an interface");} For (var j = 0, methodslen = interface. methods. length; j <methodslen; j ++) {VaR method = interface. methods [J]; If (! Ointerface [Method] | typeof ointerface [Method]! = 'Function') {Throw new error ("the given parameter is not implemented" + interface. name + "interface," + method + "not found ");}}}};

This static method is used to simulate the implements keyword in Java, but the difference is that Java uses the class actionlistener
Implements
Iactionlistener {}.
Interface. ensureimplements (oactionlistener, iactionlistener) form to ensure.

This static method accepts two parameters. The first parameter indicates the Instance Object of the interface implementation class, and the second parameter indicates the interface implementation class (of course, this is actually an object ). The method verifies the instance object.
Whether all abstract methods in the interface are implemented. If an abstract method is not implemented in this instance (the method is not found ), the instance object is not an Instance Object of the interface implementation class.

Interface instance creation and use
// Create interface var iactionlistener = new interface ("iactionlistener", ["Method1", "method2"]); // create var oactionlistener for interface instances = {Method1: function () {alert ("this is method 1") ;}, method2: function () {alert ("this is method 2") ;}; // implements confirm interface. ensureimplements (oactionlistener, iactionlistener); // call the method oactionlistener In the instance. method1 ();

This is a simple process of defining and implementing interfaces.

In JavaScript, there must be many interface simulation methods. Here is just one of the Implementation ideas.

Sorry for the improper simulation.

We hope that you can give your valuable comments.

Upgrade from ensureimplements method to implements method)

The previously defined ensureimplements (ointerface, interface1, interface2, interfacen) are
Instance to ensure, but according to common sense, an interface can be instantiated after it is implemented, but this is not a real implementation, but a check and a guarantee. To enable
The object can be instantiated using the new keyword. The original method needs to be upgraded below, so I defined the following
Method: Implements (implementsclass, interface1, interface2, interfacen). The Code is as follows:

/*** Interface implementation * @ Param {function} implementsclass class to be implemented * @ Param {object} interfacen implemented interface, an exception is thrown when the interface * @ exception {error} parameter is invalid * @ example * implements (actionlistener, iactionlistener); */var implements = function (implementsclass, interface1, interface2, interfacen) {If (arguments. length <2) {Throw new error ("interface. the ensureimplements method requires at least two parameters, and the current number of parameters is: "+ arguments. length);} // ensure imp The lementsclass type is function if (typeof arguments [0]! = "Function") {Throw new error ("the type of the implementation class must be function");} For (VAR I = 1, Len = arguments. length; I <Len; I ++) {var interface = arguments [I]; If (interface. constructor! = Interface) {Throw new error (interface + "is not an interface instance, not an interface");} // implement the interface abstract method cyclically for (var j = 0, methodslen = interface. methods. length; j <methodslen; j ++) {VaR method = interface. methods [J]; If (! Arguments [0]. Prototype [Method]) {arguments [0]. Prototype [Method] = function (){};}}}}

After this upgrade, our code can be written as follows:

// Create an interface var iactionlistener = new interface ("iactionlistener", ["Method1", "method2"]); // create an implementation class var actionlistener = function (){}; // implement implements (actionlistener, iactionlistener); // at this time, actionlistener. prototype is as follows:/* actionlistener. prototype = {Method1: function () {}, method2: function () {}}; * // the logic of the actionlistener method that can be truly filled with the empty implement is implemented. prototype = {Method1: function () {alert ("this is method 1") ;}, method2: function () {alert ("this is method 2 ");}}; // call the method oactionlistener In the instance. method1 ();

OK. Everything is normal. So far, the implements method mentioned in subsequent articles is this method. Please pay attention to it. Thank you.

 

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.