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

原型和原型链 #2

Open
AngellinaZ opened this issue Feb 8, 2018 · 1 comment
Open

原型和原型链 #2

AngellinaZ opened this issue Feb 8, 2018 · 1 comment

Comments

@AngellinaZ
Copy link
Owner

AngellinaZ commented Feb 8, 2018

JavaScript深入之从原型到原型链, 讶羽
Javascript继承机制的设计思想,阮一峰
继承和原型链, MDN

构造函数创建新对象实例

构造函数 B ( ), 用 new 构建对象实例 b

function B(name) {
  this.name = name;  // this 指向创建的实例对象,也就是b
}
var b = new B('b');
console.log(b.name) //b

prototype

  • 每个函数都有一个 prototype 属性,是函数才有的属性
  • 函数的 prototype 属性指向了一个对象,这个对象正是调用该构造函数而创建的实例的原型

 prototype

proto

  • 这是每一个JavaScript对象(除了 null )都具有的一个属性,这个属性会指向该对象的原型
B.prototype === b.__proto__ //ture

 ptoto

constructor

  • 每个原型都有一个 constructor 属性指向关联的构造函数
  • Object.getPrototypeOf( ) 获取对象的原型
B.prototype.constructor === B //ture
Object.getPrototypeOf(b) === B.prototype  //ture 

constuctor


原型链(蓝色__proto__)

 原型链
 

注意

  • 对象实例并没有constructor属性,b.constructor是顺着原型链从原型出获取了constructor属性
b.constructor === B.prototype.constructor //true
  • Object.prototype的原型是null???? 看了之后也不太懂 = =
@Sphinm
Copy link

Sphinm commented Apr 8, 2018

Object.prototype === null 表示沿着原型链查找到此结束了~

@AngellinaZ AngellinaZ changed the title 学习总结の原型和原型链 原型和原型链 Jun 4, 2019
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

2 participants