The prototype mode in JavaScript and the prototype in javascript
Objects Created in JavaScript are created in many ways, such as factory mode, constructor mode, and prototype mode:
<Pre name = "code" class = "html"> // factory mode function createPerson (name, age, job) {var o = new Object; o. name = name; o. age = age; o. job = job; o. sayName = function () {alert (this. name);} return o;} var person = createPerson ('DW ', 20, 'it ');
Its Equivalent prototype:
function Person(){}Person.prototype = {name:"dw",age:20,job:"IT",sayName:function(){alert(this.name);}};var person2 = new Person;
In Javascript, each function has the prototype attribute, which is a pointer to the prototype object and can be used to create attributes and Methods shared by all instances. In prototype mode, create an instance person3
var person3 = new Person;person2.name; //dwperson3.name; //dw
The name attribute of both person2 and person3 returns dw. The attributes and methods of the prototype object are shared by all instances of specific types. By default, each prototype object will automatically obtain a constructor attribute, which is used to point to the pointer of the function where the prototype attribute is located, for example, Person. prototype. constructor points to Person.
Alert (Person. prototype. constructor); // returns the Person constructor // Person. prototype. constructor is only a pointer to Person. It is not equal to Person. // falsealert (Person. prototype. constructor = Person) alert (Person. prototype. constructor = Person)
Shows the triangle relationship between instances, prototype objects, and constructor:
Inside each instance there is a pointer pointing to the prototype object, which is called [prototype] in the fifth edition of the ECMA-262. The instances created in the prototype mode have no direct relationship with the constructor.
Although [[prototype] cannot be accessed, you can use the isPrototypeOf () method to determine whether the object is a prototype object.
alert(Person.prototype.isPrototypeOf(person2)); //truealert(Person.prototype.isPrototypeOf(person3));//true
Because the instance has a pointer to Person. prototype, true is returned. In ECMAScript 5, you can use Object. getPrototypeOf () to return [prototype], that is, return the prototype Object.
alert(Object.getPrototypeOf(person2) == Person.prototype); //truealert(Object.getPrototypeOf(person2).name); //dw
Note: although you can use an instance to access the value in the prototype, you cannot override the value in the prototype through the instance. That is, if an attribute with the same name is added to the instance, the property with the same name is blocked.
person2.name="qs";alert(person2.name);//qs
Even if the name attribute is set to null, the name in the prototype cannot be accessed. You can use delete to delete the name attribute of an instance, and then you can re-access the name in the prototype.
<pre name="code" class="html">person2.name = null;alert(person2.name); //null
Delete person2.name; alert (person2.name); // dw
For attributes with the same name, you can use the hasOwnProperty ("propertyName") method to check whether the attribute belongs to an instance or a prototype. This method returns true only when the specified attribute exists in an instance.
Javascript prototype
The property method in the prototype is the empirical method in other languages. The static property does not belong to the object, but belongs to the class.
Understanding of the original javaScript Model
Prototype?
This is equivalent to creating a class. After adding this, the instantiated object can use the prototype method or attribute.