Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript深入系列之原型和原型链 #8

Open
archerU opened this issue May 20, 2018 · 0 comments
Open

JavaScript深入系列之原型和原型链 #8

archerU opened this issue May 20, 2018 · 0 comments

Comments

@archerU
Copy link
Owner

archerU commented May 20, 2018

构造函数

所有带 new 的函数调用。

function Foo() {
    // ...
}

var a = new Foo();

Foo 和你程序中的其他函数没有任何区别。Foo 函数本身不是构造函数,但是当且仅当使用 new 时,函数调用会变成 “构造函数调用”。

[[prototype]]

JavaScript 中的对象拥有的一个特殊的 [[prototype]] 内置属性。这个属性其实就是对其他对象的引用。

function Foo() {
    // ...
}

var a = new Foo();

Object.getPrototypeOf(a) === Foo.prototype; // true

调用 new Foo() 时会创建 a 这个对象,创建过程的其中一步是给 a 一个内部的 [[prototype]] 链接,关联到 Foo.prototype 指向的那个对象。

原型链

[[prototype]] 的作用是:如果在对象上没有找到需要的属性或者方法引用,引擎就会继续在 [[prototype]] 关联的对象上进行查找。同理,如果在后者中也没有找到需要的引用就会继续查找它的 [[prototype]],以此类推。这一系列对象的链接被称为“原型链”。

一张老图

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant