How to use object-oriented thinking to write JavaScript should be difficult for beginners. JQuery, which we often use, is actually encapsulated with object-oriented thinking, today, let's take a look at how to use interfaces in Javascript. in C # or JAVA, we should design our programs for interfaces, in C # and Java, how does IntSynta use object-oriented thinking to write JavaScript is difficult for beginners, JQuery, which we often use, is actually encapsulated with object-oriented ideas. Today we will look at how to use interfaces in Javascript. in C # or JAVA, we should design our programs for interfaces, in C # and Java, there is no corresponding mechanism in JavaScript, but Javascript is flexible. We can use its features to simulate Interface, however, we need to add some methods for the check action.
Let's take a look at the role of the next Interface: After inheriting this Interface, you must implement the method (method signature) defined in this Interface. // JavaScript cannot implement the signature constraint of the method yet.
Var Interface = function (name, methods) {if (arguments. length! = 2) {throw new Error ("the interface length is bigger than 2");} this. name = name; this. method = []; for (var I = 0; I <methods. length; I ++) {if (typeof methods [I]! = String) {throw new Error ("the method name is not string");} this. method. push (methods [I]);}/* static method in interface */Interface. ensureImplement = function (object) {if (arguments. length <2) {throw new Error ("there is not Interface or the instance");} for (var I = 1; I <arguments. length; I ++) {var interface1 = arguments [I]; if (interface1.constructor! = Interface) {throw new Error ("the argument is not interface");} for (var j = 0; j <interface1.Method. length; j ++) {var method = interface1.Method [j]; if (! Object [method] | typeof object [method]! = Function) {throw new Error ("you instance doesnt implement the interface ");}}}}
Let's analyze the code. Our current practice is to compare whether the method name in an Instance is defined in the interface.
I first define an interface (two parameters), and the second parameter is the method name in the interface. The Check method uses a simple 2-layer for loop for comparison.
Let's take a look at how to use this interface:
Var Person = new Interface ("Person", ["GetName", "GetAge"]); var Man = function (name, age) {this. name = name; this. age = age;} Man. prototype = {GetName: function () {return this. name ;}, // GetAge: function () {return this. age ;}} var test = function (instance) {Interface. ensureImplement (instance, Person); var name = instance. getName (); alert (name);} test (new Man ("Alan", 20 ));
If we comment out the above GetAge method, an error will occur during execution. This method was not implemented during ensureImplement.
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