The concept of class in JS
Class, which is actually a function, is also the constructor of this class, new creates an instance of the class, and the new object has a property method.
Method is also a special kind of object.
Methods of the class
The method of initializing the instance in the constructor method (that is, writing the method directly in the constructor method, and new instantiation) is not recommended, consumes memory (every time the instantiation is duplicated content, occupy some memory, it is neither environmental protection, also lacks efficiency).
All instances are common, creating multiple instances does not produce new function, it is recommended to define the method of the instance in the prototype of the class.
The methods in prototype are common to all instances.
1. Modeled after jquery encapsulation class
anonymous functions
(function () { //}) (); var function (i) { this. id =function (i) { return New Id (i);}; Console.log ($ (' main '));
functionCat (name, color) { This. Name =name; This. color =color;}varCAT1 =NewCat (' da Mao ', ' Yellow ');varCAT2 =NewCat (' Er Mao ', ' black '); Cat.prototype.a= ' AAA '; Cat.prototype.type= ' Cat animals '; Cat.prototype.eat=function() {alert (' Eat mice ');}; Cat1.eat (); Cat2.eat (); Console.log (cat1.name); Console.log (Cat2.color) ;//CAT1 and Cat2 automatically contain a constructor attribute that points to their constructors. Console.log (Cat1.constructor = =Cat); Console.log (Cat2.constructor==Cat);//JavaScript also provides a instanceof operator that validates the relationship between a prototype object and an instance object. Console.log (CAT1instanceofCat);Try{Console.log (ainstanceofCat);} Catch(e) {console.log (e);}
The so-called "constructor" is actually a normal function, but the this variable is used internally. Using the new operator on a constructor enables you to generate an instance, and the this variable is bound to the instance object.
JavaScript specifies that each constructor has a prototype property that points to another object. All properties and methods of this object are inherited by an instance of the constructor.
Verification method of prototype mode
1. isPrototypeOf () Determine the relationship between a prototype object and an instance
2. hasOwnProperty () Determine whether a property is a local property or a property that inherits from a prototype object
3. In determining whether an object
functionCat (name, color) { This. Name =name; This. color =color;} Cat.prototype.type= ' Cat animals ';varCAT1 =NewCat (' da Mao ', ' Yellow ');varCAT2 =NewCat (' Er Mao ', ' black '); Console.log (Cat.prototype.isPrototypeOf (CAT1)); Console.log (Cat.prototype.isPrototypeOf (CAT2)); Console.log (Cat1.hasownproperty (' Name ')); Console.log (Cat2.hasownproperty (' Type ')); Console.log (' Name 'inchcat1); Console.log (' Type 'inchCAT1);
Not finished: Inheritance of constructors: http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance.html
Inheritance of non-constructors: http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance_continued.html
JS Package (JS plug-in package)