Class Definition and Inheritance of JavaScript
// Create the object var obj = {property: "this is property", method: function () {return "I'm a method ";}, // toString will be called when the Object is converted into a string, which can be used as the description of the Object toString: function () {return "I'm a Object ";}} // In the JavaScript specification, any Object created by {} belongs to the Object class and allows dynamic objects. // to enable JavaScript to have an object-oriented implementation mechanism, there are two indispensable elements: constructor function and prototype object // function Rectanle (width, height) in front of the upper-case class name {// Add this. width = width; this. height = height; // define the method this. area = function () {// you must add this return this to obtain the instance variable. width * this. height ;}// in JavaScript, a function is also an object with its own attributes and Methods. Instances created using Rectanle have three attributes: width, height, area // JavaScript each object created through the constructor contains a fixed property prototype function Rectanle2 (width, height) {this. width = width; this. height = height; Rectanle2.prototype. area = function () {return this. width * this. height ;}} var rect = new Rectanle2 (); // You can output console.info ("the rectanle area is:" + rect. area (); // when JavaScript encounters a rect. when area () is used, you can find the attribute area on the object and call the area () method directly, if 100 such instances are created and 100 identical areas are in the memory, this is obviously unreasonable. If the method is defined in the prototype of Rectanle, when the JavaScript engine encounters a rect. area () // This attribute is not found in the object. JavaScript will continue to search for the prototype attribute of rect. The prototype of rect is the prototype pointing to Rectanle, javaScript // engine in Rectanle. if area () is found on prototype, this method is called, even if 100 such instances, no area exists in the memory // defines the Class, Class method, and class attribute function Person () {} Person. id = 0; Person. createAccount = function () {Person. id ++; return new Person ();} var p1 = Person. createAccount (); var p2 = Person. createAccount (); console.info ("the is:" + Person. id); // The JavaScript1.5 version specification does not contain abstract classes, interfaces, and multi-inheritance. It only has a single inheritance. The inheritance must use prototype to implement function Person (name, email) {this. name = name; this. email = email;} Person. prototype. getInfo = function () {return "name:" + this. name + "email:" + this. email;} function Employee (name, email, title) {// like super () in Java, you must use call to explicitly define scope as the Person of the Employee instance. call (this, name, email); this. title = title;} // If You Want To Inherit the Person, the prototype of the subclass must be assigned as the instance of the parent class. prototype = new Person (); var employee = new Employee ("fuyuwei", "fuyuwei@outlook.com", "info"); console.info ("Employee info is:" + employee. getInfo ());