Home >Web Front-end >JS Tutorial >How to understand JavaScript prototypes
Prototype is the basis of inheritance in JavaScript. JavaScript inheritance is prototype-based inheritance. The prototype object of a function is in JavaScript. If we create a function A, then the browser will create an object B in the memory. .
Prototype is the basis of inheritance in Javascript. Inheritance in JavaScript is prototype-based inheritance.
1.1 Prototype object of function
In JavaScript, we create a function A ( means declaring a function), then browse The processor will create an object B in memory, and each function will have an attribute prototype pointing to this object by default (ie: the value of the prototype attribute is this object) . This object B is the prototype object of function A, referred to as the prototype of the function. By default, this prototype object B will have a property constructor pointing to this function A (which means: the value of the constructor property is function A).
Look at the following code:
<body> <script type="text/javascript"> /* 声明一个函数,则这个函数默认会有一个属性叫 prototype 。而且浏览器会自动按照一定的规则 创建一个对象,这个对象就是这个函数的原型对象,prototype属性指向这个原型对象。这个原型对象 有一个属性叫constructor 执行了这个函数 注意:原型对象默认只有属性:constructor。其他都是从Object继承而来,暂且不用考虑。 */ function Person () { } </script> </body>
The following figure describes what happens after declaring a function:
1.2 Using the constructor Creating an object
When using a function as a constructor (theoretically any function can be used as a constructor) and using new to create an object, the object will have a default invisible attribute. to point to the prototype object of the constructor. We generally use [[prototype]] to represent this invisible attribute, but this attribute cannot be accessed directly.
Look at the code below:
<body> <script type="text/javascript"> function Person () { } /* 利用构造函数创建一个对象,则这个对象会自动添加一个不可见的属性 [[prototype]], 而且这个属性 指向了构造函数的原型对象。 */ var p1 = new Person(); </script> </body>
Observe the diagram below:
Explanation:
As you can see from the above illustration, although the Person constructor is used to create the p1 object, after the object is created, the p1 object actually has nothing to do with the Person constructor. p1 The [[ prototype ]] property of the object points to the prototype object of the Person constructor.
If you use new Person() to create multiple objects, multiple objects will point to the prototype object of the Person constructor at the same time.
We can manually add properties and methods to this prototype object, then p1, p2, p3... these objects will share the properties and methods added in the prototype.
If we access an attribute name in p1, if it is found in the p1 object, it will be returned directly. If it is not found in the p1 object, search directly in the prototype object pointed to by the [[prototype]] attribute of the p1 object, and return if found. (If it is not found in the prototype, continue to look up the prototype-prototype chain of the prototype. More on this later).
If an attribute name is added through the p1 object, the attribute name in the prototype is blocked for the p1 object. In other words: there is no way to access the prototype attribute name in p1.
You can only read the value of the attribute name in the prototype through the p1 object, but you cannot modify the value of the attribute name in the prototype. p1.name = "李思"; It does not modify the value in the prototype, but adds an attribute name to the p1 object.
Look at the following code:
<body> <script type="text/javascript"> function Person () { } // 可以使用Person.prototype 直接访问到原型对象 //给Person函数的原型对象中添加一个属性 name并且值是 "张三" Person.prototype.name = "张三"; Person.prototype.age = 20; var p1 = new Person(); /* 访问p1对象的属性name,虽然在p1对象中我们并没有明确的添加属性name,但是 p1的 [[prototype]] 属性指向的原型中有name属性,所以这个地方可以访问到属性name 就值。 注意:这个时候不能通过p1对象删除name属性,因为只能删除在p1中删除的对象。 */ alert(p1.name); // 张三 var p2 = new Person(); alert(p2.name); // 张三 都是从原型中找到的,所以一样。 alert(p1.name === p2.name); // true // 由于不能修改原型中的值,则这种方法就直接在p1中添加了一个新的属性name,然后在p1中无法再访问到 //原型中的属性。 p1.name = "李四"; alert("p1:" + p1.name); // 由于p2中没有name属性,则对p2来说仍然是访问的原型中的属性。 alert("p2:" + p2.name); // 张三 </script> </body>
2.1 prototype attribute
prototype exists in the constructor (in fact, it exists in any function, but we don’t pay attention to prototype when it is not a constructor), it points to The prototype object of this constructor.
Refer to the previous diagram.
2.2 constructor attribute
The constructor attribute exists in the prototype object, and it points to the constructor function
Look at the following code:
<script type="text/javascript"> function Person () { } alert(Person.prototype.constructor === Person); // true var p1 = new Person(); //使用instanceof 操作符可以判断一个对象的类型。 //typeof一般用来获取简单类型和函数。而引用类型一般使用instanceof,因为引用类型用typeof 总是返回object。 alert(p1 instanceof Person); // true </script>
We can specify a new object using the Person.prototype property as the prototype object of Person as needed.
But there is a problem at this time. The constructor property of the new object no longer points to the Person constructor.
Look at the following code:
<script type="text/javascript"> function Person () { } //直接给Person的原型指定对象字面量。则这个对象的constructor属性不再指向Person函数 Person.prototype = { name:"志玲", age:20 }; var p1 = new Person(); alert(p1.name); // 志玲 alert(p1 instanceof Person); // true alert(Person.prototype.constructor === Person); //false //如果constructor对你很重要,你应该在Person.prototype中添加一行这样的代码: /* Person.prototype = { constructor : Person //让constructor重新指向Person函数 } */ </script>
2.3 __proto__ attribute (note: there are 2 underscores on the left and right)
Create with the constructor method After a new object is created, there will be an inaccessible attribute [[prototype]] in this object by default, which points to the prototype object of the constructor.
But in some browsers, access to this attribute [[prototype]] is also provided (chrome browser and Firefox browser. IE browser does not support it). Access method: p1.__proto__
But developers should try not to access in this way, because careless operation will change the inheritance prototype chain of this object.
<script type="text/javascript"> function Person () { } //直接给Person的原型指定对象字面量。则这个对象的constructor属性不再指向Person函数 Person.prototype = { constructor : Person, name:"志玲", age:20 }; var p1 = new Person(); alert(p1.__proto__ === Person.prototype); //true </script>
2.4 hasOwnProperty() method
大家知道,我们用去访问一个对象的属性的时候,这个属性既有可能来自对象本身,也有可能来自这个对象的[[prototype]]属性指向的原型。
那么如何判断这个对象的来源呢?
hasOwnProperty方法,可以判断一个属性是否来自对象本身。
<script type="text/javascript"> function Person () { } Person.prototype.name = "志玲"; var p1 = new Person(); p1.sex = "女"; //sex属性是直接在p1属性中添加,所以是true alert("sex属性是对象本身的:" + p1.hasOwnProperty("sex")); // name属性是在原型中添加的,所以是false alert("name属性是对象本身的:" + p1.hasOwnProperty("name")); // age 属性不存在,所以也是false alert("age属性是存在于对象本身:" + p1.hasOwnProperty("age")); </script>
所以,通过hasOwnProperty这个方法可以判断一个对象是否在对象本身添加的,但是不能判断是否存在于原型中,因为有可能这个属性不存在。
也即是说,在原型中的属性和不存在的属性都会返回fasle。
如何判断一个属性是否存在于原型中呢?
2.5 in 操作符
in操作符用来判断一个属性是否存在于这个对象中。但是在查找这个属性时候,现在对象本身中找,如果对象找不到再去原型中找。换句话说,只要对象和原型中有一个地方存在这个属性,就返回true
<script type="text/javascript"> function Person () { } Person.prototype.name = "志玲"; var p1 = new Person(); p1.sex = "女"; alert("sex" in p1); // 对象本身添加的,所以true alert("name" in p1); //原型中存在,所以true alert("age" in p1); //对象和原型中都不存在,所以false </script>
回到前面的问题,如果判断一个属性是否存在于原型中:
如果一个属性存在,但是没有在对象本身中,则一定存在于原型中。
<script type="text/javascript"> function Person () { } Person.prototype.name = "志玲"; var p1 = new Person(); p1.sex = "女"; //定义一个函数去判断原型所在的位置 function propertyLocation(obj, prop){ if(!(prop in obj)){ alert(prop + "属性不存在"); }else if(obj.hasOwnProperty(prop)){ alert(prop + "属性存在于对象中"); }else { alert(prop + "对象存在于原型中"); } } propertyLocation(p1, "age"); propertyLocation(p1, "name"); propertyLocation(p1, "sex"); </script
3.1 原型模型创建对象的缺陷
原型中的所有的属性都是共享的。也就是说,用同一个构造函数创建的对象去访问原型中的属性的时候,大家都是访问的同一个对象,如果一个对象对原型的属性进行了修改,则会反映到所有的对象上面。
但是在实际使用中,每个对象的属性一般是不同的。张三的姓名是张三,李四的姓名是李四。
==但是,这个共享特性对 方法(属性值是函数的属性)又是非常合适的。==所有的对象共享方法是最佳状态。这种特性在c#和Java中是天生存在的。
3.2 构造函数模型创建对象的缺陷
在构造函数中添加的属性和方法,每个对象都有自己独有的一份,大家不会共享。这个特性对属性比较合适,但是对方法又不太合适。因为对所有对象来说,他们的方法应该是一份就够了,没有必要每人一份,造成内存的浪费和性能的低下。
<script type="text/javascript"> function Person() { this.name = "李四"; this.age = 20; this.eat = function() { alert("吃完东西"); } } var p1 = new Person(); var p2 = new Person(); //每个对象都会有不同的方法 alert(p1.eat === p2.eat); //fasle </script>
可以使用下面的方法解决:
<script type="text/javascript"> function Person() { this.name = "李四"; this.age = 20; this.eat = eat; } function eat() { alert("吃完东西"); } var p1 = new Person(); var p2 = new Person(); //因为eat属性都是赋值的同一个函数,所以是true alert(p1.eat === p2.eat); //true </script>
但是上面的这种解决方法具有致命的缺陷:封装性太差。使用面向对象,目的之一就是封装代码,这个时候为了性能又要把代码抽出对象之外,这是反人类的设计。
3.3 使用组合模式解决上述两种缺陷
原型模式适合封装方法,构造函数模式适合封装属性,综合两种模式的优点就有了组合模式。
<script type="text/javascript"> //在构造方法内部封装属性 function Person(name, age) { this.name = name; this.age = age; } //在原型对象内封装方法 Person.prototype.eat = function (food) { alert(this.name + "爱吃" + food); } Person.prototype.play = function (playName) { alert(this.name + "爱玩" + playName); } var p1 = new Person("李四", 20); var p2 = new Person("张三", 30); p1.eat("苹果"); p2.eat("香蕉"); p1.play("志玲"); p2.play("凤姐"); </script>
前面讲到的组合模式,也并非完美无缺,有一点也是感觉不是很完美。把构造方法和原型分开写,总让人感觉不舒服,应该想办法把构造方法和原型封装在一起,所以就有了动态原型模式。
动态原型模式把所有的属性和方法都封装在构造方法中,而仅仅在需要的时候才去在构造方法中初始化原型,又保持了同时使用构造函数和原型的优点。
看下面的代码:
<script type="text/javascript"> //构造方法内部封装属性 function Person(name, age) { //每个对象都添加自己的属性 this.name = name; this.age = age; /* 判断this.eat这个属性是不是function,如果不是function则证明是第一次创建对象, 则把这个funcion添加到原型中。 如果是function,则代表原型中已经有了这个方法,则不需要再添加。 perfect!完美解决了性能和代码的封装问题。 */ if(typeof this.eat !== "function"){ Person.prototype.eat = function () { alert(this.name + " 在吃"); } } } var p1 = new Person("志玲", 40); p1.eat(); </script>
推荐教程:《JS教程》
The above is the detailed content of How to understand JavaScript prototypes. For more information, please follow other related articles on the PHP Chinese website!