当前位置:网站首页>Prototype and prototype chain
Prototype and prototype chain
2022-06-11 06:56:00 【Xiaoman's code world】
[[prototype]]
Javascript Object has a special [[prototype]] Built in properties , It is actually a reference to other objects . Almost all objects are created [[prototype]] Will be given a non null value .
Object.create() Create an object and put the [[prototype]] Relate to the specified object .
All ordinary [[prototype]] The chain will eventually point to the built-in Object.prototype,
class
function Foo() {
}
Foo.prototype;
var a = new Foo();
Object.getPrototypeOf(a) == Foo.prototype // true
Foo.prototype.constructor == Foo // true
a.constructor == Foo // true
call new Foo() Will create a new object a, to a One [[prototype]] link , Related to Foo.prototype Object to point to ;
a.constructor A function that points to an associated object , actually a There is no such thing as constructor attribute ,
.constructor Reference delegated to Foo.prototype, and Foo.prototype.constructor Point to Foo.a.constructor Just by default [[prototype]] Delegation points to Foo, This has nothing to do with construction .
Foo.prototype Of .constructor Property is just Foo Default attribute when function is declared , If you create a new object and replace the function default .prototype Object reference , Then the new object does not automatically get .constructor attribute .
function Foo() {
}
Foo.prototype = {};
var a = new Foo();
a.constructor == Foo // false
a.constructor == Object // true
a did not .constructor attribute , So I will entrust [[prototype]] On the chain prototype, But this object doesn't have .constructor attribute ( default Foo.prototype It has this property ), So I will continue to entrust , This time it will be entrusted to the top of the chain Object.prototype, Of this object .constructor Property points to the built-in Object function .
stay JavaScript in , We don't put an object (‘ class ’) Copy to another object (‘ example ’), Just connect them .
Prototype inheritance
function Foo(name) {
this.name = name;
}
Foo.prototype.myName = function () {
return this.name;
}
function Bar(name, label) {
Foo.call(this, name)
this.label = label;
}
Bar.prototype = Object.create(Foo.prototype);
Bar.prototype.myLabel = function () {
return this.label;
}
var a = new Bar('a', 'obj a');
a.myName(); // 'a'
a.myLabel(); // 'obj a'
call Object.create() A new object will be created out of thin air and the [[prototype]] Relate to the specified object (Foo.prototype). Bar.prototype = Object.create(Foo.prototype); Create a new Bar.prototype And associate it to Foo.prototype, Put the original Bar.prototype Abandon .
There are two ways to write in question :
// Does not create an association to Bar.Fprototype New object , Just let Bar.prototype quote Foo.prototype object ,
// When executed Bar.prototype.myLabel It will be modified directly Foo.prototype Object itself .
Bar.prototype = Foo.prototype;
// Although it will create an association to Bar.Fprototype New object , But use Foo Constructor call for , If Foo Functions have side effects , It will affect Bar The offspring of , The consequences are unimaginable .
Bar.prototype = new Foo();
stay ES6 Before, we could only set up __proto__ Attribute implementation , But this method is not standard and incompatible .ES6 Added auxiliary functions Object.setPrototypeOf(), Associations can be modified in a standard and reliable way .
Bar.prototype = Object.create(Foo.prototype)
Object.setPrototypeOf(Bar.prototype, Foo.prototype)
— Reference resources 《 You don't know? Javascript On 》
边栏推荐
- 通过 Ingress 进行灰度发布
- UEFI查找PCI设备
- Dynamic import
- 争议很大的问题
- 搜狐员工遭遇工资补助诈骗 黑产与灰产有何区别 又要如何溯源?
- A highly controversial issue
- Won't virtual DOM be available in 2022? Introduction to virtual Dom and complete implementation of diff and patch
- 1266_FreeRTOS调度器启动代码实现分析
- ESP32学习笔记(49)——ESP-WIFI-MESH接口使用
- LEARNING TARGET-ORIENTED DUAL ATTENTION FOR ROBUST RGB-T TRACKING
猜你喜欢

Duality-Gated Mutual Condition Network for RGBT Tracking

你知道IT人才外派服务报价是怎样的么?建议程序员也了解下

Unity 全景漫游过程中使用AWSD控制镜头移动,EQ控制镜头升降,鼠标右键控制镜头旋转。

JS two methods to determine whether there are duplicate values in the array

Deep Attentive Tracking via Reciprocative Learning

Biweekly investment and financial report: capital rush yuan universe game

Won't virtual DOM be available in 2022? Introduction to virtual Dom and complete implementation of diff and patch

河南高考VS天津高考(2008年-2021年)

Use of qscriptengine class

双周投融报:资本抢滩元宇宙游戏
随机推荐
Required reading 1: the larger the pattern, the better they know how to speak
UEFI finding PCI devices
text-overflow失效
latex 各种箭头/带文字标号的箭头/可变长箭头
fatal: refusing to merge unrelated histories
saltstack部署lnmp
Biweekly investment and financial report: capital rush yuan universe game
Redux learning (I) -- the process of using Redux
【Matlab图像融合】粒子群优化自适应多光谱图像融合【含源码 004期】
instanceof到底是怎样判断引用数据类型的!
VTK vtkplane and vtkcutter use
开源漫画服务器Mango
Common troubleshooting tools and analysis artifacts are worth collecting
JS implementation of Hill sort of graphic insertion sort [with source code]
[MATLAB image encryption and decryption] chaotic sequence image encryption and decryption (including correlation test) [including GUI source code 1862]
Cross-Modal Pattern-Propagation for RGB-T Tracking
Redux learning (II) -- encapsulating the connect function
Common modules of saltstack
无心剑汉英双语诗001.《爱》
Dynamic import