The object constructor or object literal can be used to create objects, but there are obvious drawbacks: creating many objects with the same interface creates a lot of duplicate code.
Factory mode
// Factory mode function Createdog (name,age) { varnew Object (); = name; = Age ; function () { alert (age); }; return o; } var dog1 = Createdog ("Bob", "one"); var dog2 = Createdog ("Ann", "5");
Factory mode solves the problem of creating multiple similar objects, but does not solve the problem of object recognition (that is, how to know the type of an object).
Constructor mode
// constructor Mode function Dog (name,age) { this. Name = name; this. Age = Age ; This function () { alert (age); }; } var New Dog ("Bob", "one"); var New Dog ("Ann", "5");
In this example, the method and properties are assigned directly to the This object. In this case, the D used by the function name Dog is a capital letter, and the constructor should always start with an uppercase character, primarily to distinguish it from other functions in Es, because the constructor itself is a function, but it can be used to create objects.
To create a new instance of the constructor, you must use the new operator.
Here are the steps to create
1. Create a new object;
2. Assign the scope of the constructor to the new object (this points to the new object);
3. Execute the code of the constructor function;
4. Returns the new object.
Dog1 and Dog2 each kept a different instance of dog. Both objects have a constructor (constructor) property that points to the dog.
// true // true
The constructor property is initially used to identify the object type. It is more reliable to detect the object type or use the instanceof operator.
instanceof // true instanceof//true
Dog1 is also an instance of object, because all objects inherit from object.
Constructors can also be called as normal functions.
// as a normal function Dog ("Bob", "one-by-one"//"one-by-one"/// in another object scope call var o =new Object ();D og.call (o,"Bob", "one"// One
Disadvantages of Constructors:
Each method is recreated on each instance, which consumes more memory and affects efficiency.
Functions created in this way cause different scope chains and identifier resolution. Therefore, the functions named on different instances are not equal.
// false
Prototype mode
Each function we create has a prototype (prototype) attribute, which is a pointer to an object.
The benefit of using the prototype object is that you do not have to construct the information for the object instance defined in the function, but instead add the information directly to the prototype object.
functionDog () {} Dog.prototype.name= "Bob"; Dog.prototype.age= "11"; Dog.prototype.jump=function() {alert ("Skip");}; varDog1 =NewDog (); Dog1.jump (); //Jump varDOG2 =NewDog (); Dog2.jump (); //Jumpalert (dog1.name); Alert (Dog1.jump= = Dog2.jump);//true
Validation of prototype objects
isprototypeof () :
After a custom constructor is created, the prototype object defaults to only the constructor property, and the other methods inherit from object. When the constructor is called to create a new instance, the instance inside will contain a pointer
Point to the prototype object. However, this property is completely invisible to the script, which exists between the instance and the prototype object of the constructor.
This can be determined by the isprototypeof () method.
// true
object.getprototypeof () :
// true // "Bob"
This method can get a prototype of the object.
hasOwnProperty ():
The hasOwnProperty () method can detect whether a property exists in an instance or in a prototype.
functionDog () {} Dog.prototype.name= "Bob"; Dog.prototype.age= "11"; Dog.prototype.jump=function() {alert ("Skip");}; varDog1 =NewDog (); varDOG2 =NewDog (); Alert (Dog1.hasownproperty ("Name"));//falseDog1.name= "Gina"; alert (dog1.name); //"Gina"Alert (Dog1.hasownproperty ("name"));//trueAlert (Dog2.hasownproperty ("name"));//false--from prototype
Therefore, only a given property exists in the object instance to return True, where the name Dog1 is masked by a new value. This is the result of an upward search of the scope chain.
JavaScript Advanced Programming (object-oriented programming) 1