In the previous article I mainly introduce the creation of objects in JavaScript, but, in the object creation process has a lot of knowledge we have not learned, that is the prototype, what is the prototype? Here I would like to use their own understanding to tell you that there is no place you can give me advice.
First of all, the prototype is the object, the properties and methods of this object can be shared, who is shared? is to be shared by the instances (when we create an object and then use that object to instantiate many instances).
Each object we create has a prototype (prototype) attribute, which is automatically created by the system, and this property (prototype) gives you direct access to the object's prototype object. An example is shown below.
function Box () {} //Declare a constructor Box.prototype.name = ' Beauty '; Add attribute Box.prototype.age = 20 in the prototype; Box.prototype.run = function () { //Add method in Prototype return this.name + This.age + ' Look at me ... ';};
Where Box.prototype represents a prototype of the box () object, box.prototype.age=20, the age property of the prototype object that is represented to box () is assigned a value of 20. Now, declare an instance of the two box () object to see.
var box1 = new Box (), var box2 = new box (), alert (Box1.run = = Box2.run); Returns true to indicate that both of the run reference addresses are the same.
As can be seen from the above figure, instances Box1 and Box2 can access the box prototype through the Constructor object box, while some of the methods and properties placed in box prototype are shared by Box1 and Box2.
There are two methods under prototype: Apply () and call (), each of which contains the two non-inherited methods. The purpose of both methods is to invoke the function in a specific scope, which is actually equivalent to setting the value of the This object in the function body. My understanding is that a pass parameter is used to impersonate an object, and one is to change the scope of a function. Object Impersonation My understanding is that a object that is impersonating a B object will have properties or methods of the B object, but the properties and methods of the B object reference type cannot be impersonated. Actually the call () and the Apply () method are the same, here I only introduce the call () method.
1. The call () method of the prototype (changing the scope of the function)
<span style= "color: #000000;" >var color = ' red '; or Window.color = ' red ', or var box = {color: ' Blue '};function Saycolor () {alert (this.color);} Saycolor (); The scope returns red in window. Saycolor.call (this); The scope returns red in window. Saycolor.call (window); The scope returns red in window. Saycolor.call (box); The scope returns blue within the box object. </span>
2. The call () method of the prototype (object impersonating)
function Box (name,age) {this.name=name;this.age=age;run=function () {return this.name+this.age+ "look at Me ..."}} function Desk (name1,age1) {box.call (this,name1,age1); Object impersonating a call, which is also the function of passing parameters. }var a =new Desk (' Liu Yifei ', N); alert (a.name); Returns Liu Yifei alert (a.age); Returns 15alert (A.run ()); No return value, because this is a reference type, cannot use an object to impersonate a call, if you want to call, you need to assign the new box () to Desk's prototype code as follows//desk.prototype=new box () //This method is the object box () The instance and the prototype information are all assigned to the desk object.
In addition, the prototype mode is the execution process: 1, first find the constructor instance of the property or method, if any, immediately return, 2, if not in the constructor instance, then go to its prototype object to find, if there is, return. This is not a matching example.
Summary
Creating an instance with a constructor that passes parameters allows different instances to initialize different values, and prototypes play a big role in the inheritance of objects, and if you want to know the role that prototypes play in inheriting objects, see my next blog, JavaScript-object-oriented inheritance.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"JavaScript"--object-oriented prototypes