JavaScript implementation class and inheritance

Source: Internet
Author: User

1. Preface some people think JavaScript is a process-oriented language. Because basically all functions are written and called. ==> This idea is incorrect. The founder of JS is Brendan Eich. When JavaScript was founded, Java and object-oriented design had become popular. In May 1995, Netscape made a decision. In the future, the Web Page scripting language must "look similar to Java", but it is simpler than Java, so that non-professional web page authors can get started quickly. Based on this, the design ideas are as follows: (1) using the basic syntax of C language; (2) using the Data Type and memory management of Java language; (3) using the Scheme language, promote functions to the "first class" status. (4) use prototype-based inheritance mechanisms based on the Self language. Because the author is not interested in the java language. Therefore, the Javascript language is actually a mix of the two language styles-(simplified) functional programming + (simplified) object-oriented programming. interestingly, the author is not very satisfied with the Language = "" I hate Javascript rather than hate it. It is a one-night stand product of C language and Self language. "2. Class LibraryJS does not have the concept of Class. prototype is used to implement the inheritance mechanism. For programmers who are used to the class usage mechanism of Java and C #, it is not very easy to use. John Resig, author of JQuery, also provides a library. Class and extend can be used in JS. Class. js [javascript]/** Simple JavaScript Inheritance * By John Resig http://ejohn.org/* MIT Licensed. **************************************** * ************** Example Usage ********************** * ****************************** var Person = Class. extend ({init: function (isDancing) {this. dancing = isDancing;}, dance: function () {return this. dancing ;}}); var Ninja = Person. extend ({init: fun Ction () {this. _ super (false) ;}, dance: function () {// Call the inherited version of dance () return this. _ super () ;}, swingSword: function () {return true ;}}); var p = new Person (true); p. dance (); // => true var n = new Ninja (); n. dance (); // => false n. swingSword (); // => true // shocould all be true p instanceof Person & p instanceof Class & n instanceof Ninja & n instanceof Person & n insta Nceof Class ************************************** **************** // use red by base2 and Prototype (function () {var fnTest =/xyz /. test (function () {xyz ;})? /\ B _super \ B /:/. * // The base Class implementation (does nothing) this. class = function () {}; // Create a new Class that inherits from this class Class. extend = function (prop) {var _ super = this. prototype; // Instantiate a base class (but only create the instance, // don't run the init constructor) initializing = true; var prototype = new this (); initializing = false; // Copy the properties ove R onto the new prototype for (var name in prop) {// Check if we're overwriting an existing function prototype [name] = typeof prop [name] = "function" & typeof _ super [name] = "function" & fnTest. test (prop [name])? (Function (name, fn) {return function () {var tmp = this. _ super; // Add a new. _ super () method that is the same method // but on the super-class this. _ super = _ super [name]; // The method only need to be bound temporarily, so we // remove it when we're done executing var ret = fn. apply (this, arguments); this. _ super = tmp; return ret ;};} (name, prop [name]): prop [name] ;}// The dummy class constru Ctor function Class () {// All construction is actually done in the init method if (! Initializing & this. init) this. init. apply (this, arguments);} // Populate our constructed prototype object Class. prototype = prototype; // Enforce the constructor to be what we recommend CT Class. prototype. constructor = Class; // And make this class extendable Class. extend = arguments. callee; return Class ;};}) (); 3. analyze the above Class. js implementation mechanism is actually very simple. If you are not clear about these concepts using Prototype, argumnet, apply, and callee of JS, refer:

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.