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原型链的简单总结 #21

Open
varHarrie opened this issue Jan 30, 2018 · 0 comments
Open

JavaScript原型链的简单总结 #21

varHarrie opened this issue Jan 30, 2018 · 0 comments
Milestone

Comments

@varHarrie
Copy link
Owner

varHarrie commented Jan 30, 2018

JavaScript中万物皆是对象,且对象细分为普通对象函数对象

凡是通过new Function()创建的均为函数对象,如ObjectFunctionNumberBooleanStringArrayRegExp等等,其余都是普通对象

  1. 函数对象__proto__均指向Function.prototype,且为空函数function () {}
  2. 普通对象__proto__均指向该对象构造函数prototype
  3. Function.prototype.__proto__ === Object.prototype
  4. Object.prototype.__proto__ = null

对于普通对象:

function Person () {}
Person.prototype.say = function () {}

var p = new Person()
p.say()

Person相当于是构造函数,pPerson的实例,并且继承p的原型p.__proto__,即Person.prototype的所有属性。

当然,Person本身也是函数对象,这里我们考虑的是Person的实例p

p.constructor === Person // function Person () {}
p.constructor === Person.prototype.constructor // function Person () {}
p.__proto__ === Person.prototype // {say: function () {}}
p.__proto__ === p.constructor.prototype // {say: function () {}}
p.__proto__.__proto__ === Person.prototype.__proto__ === Object.prototype

对于函数对象:

var f = new Function()

f.__proto__ === Function.prototype // function () {}
f.__proto__.__proto__ === Function.prototype.__proto__ === Object.prototype

注意:

当我们重写原型对象,而不是修改原型对象时:

function Ghost () {}
Ghost.prototype = {} // 直接赋值一个对象

var g = new Ghost()

g.__proto__ === Ghost.prototype
g.__proto__ !== g.constructor.prototype
g.__proto__ === Object.prototype

这时,g.constructor指向的是Object,可以通过指定constructor避免这种情况:

function Ghost () {}
Ghost.prototype = {
  constructor: Ghost
}

var g = new Ghost()

g.__proto__ === Ghost.prototype === g.constructor.prototype

本文参考最详尽的 JS 原型与原型链终极详解,没有「可能是」进行总结,感谢原作者。

@varHarrie varHarrie added this to the Posts milestone Aug 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant