Comments (1)
准确的说,实例的 __proto__(是 __proto__不是 __protote__ 哈~) 和 Person.prototype 都指向一个对象,这个对象就是实例的原型对象,实例在查找属性或者方法的时候,如果找不到,就会到该实例的原型对象中进行查找,同一个构造函数的所有实例都指向一个原型对象,如果你不修改它的话。
为了更好的理解图中的例子,我们需要先了解下 new 的工作原理,在 《JavaScript深入之new的模拟实现》 中有详细的介绍,我们直接看最后的代码:
function objectFactory() {
var obj = new Object(),
Constructor = [].shift.call(arguments);
obj.__proto__ = Constructor.prototype;
var ret = Constructor.apply(obj, arguments);
return typeof ret === 'object' ? ret : obj;
};
当使用 new 的时候,步骤如下:
- 创建新对象
- 设置该对象的原型对象为构造函数的 prototype
- 将 this 指向新对象,这一步使用了 apply,表明函数此时会被执行一遍
- 返回新对象
我们再看文中的这个例子:
当执行 var person1 = new Person('kevin') 的时候,按照以上所说的执行步骤,就会创建一个新对象,假设就叫 obj 吧,然后就将 obj 的原型设置为 person.prototype 所指向的那个对象,我们命名为 O 吧,然后我们才将 this 指向 obj,这个过程中,我们会执行函数中的内容,也就会更改 Person.prototype,可是我们已经将 obj 的原型对象指向了 O,更改 Person.prototype 也不会改变这个关系了,只能对下一个 new 的对象才会生效,我们再简化下这个过程:
// {value: 1} 相当于原型对象 , obj.a 指向 {value: 1},就像 Person.prototype 指向原型对象
var obj = {
a: {value: 1}
}
var b = {}
b.protoValue = obj.a; // 相当于设置了实例的 __proto__ 为 Person.protoype
obj.a = {value: 2} // 相当于你更改了 Person.prototype
console.log(b.protoValue) // {value: 1} // 即便你更改了 Person.prototype,已经建立的关系无法被删除
var c = {}
c.protoValue = obj.a;
console.log(c.protoValue) // {value: 2} // 对下一个才会生效
所以文中的这个例子,person1 的 proto 还是指向更改之前的 Person.prototype 指向的那个对象,所以不会有 getName 方法。
from blog.
Related Issues (20)
- 大佬您好,看完大有收获,但是遇到了个问题,不太会解释它的执行上下文入栈出栈顺序,关于尾递归,顺序是怎样呢? HOT 1
- 冴羽答读者问:如何在工作中打造影响力,带动同事?
- 无
- 冴羽答读者问:如何学习更有计划性、提升更稳更快? HOT 4
- 冴羽答读者问:过程比结果重要吗? HOT 1
- 冴羽答读者问:冴羽,你为什么写起了鸡汤? HOT 3
- 聊聊 npm 的语义化版本(Semver) HOT 4
- How to create Backlinks 😤
- 思考题第2题
- 可以理解为原型是prototype,原型链是通过__proto__ 链接起来的吗
- React 之 createElement 源码解读 HOT 8
- React 之元素与组件的区别 HOT 1
- React 之 Refs 的使用和 forwardRef 的源码解读
- React 之 Context 的变迁与背后实现 HOT 1
- 第一段不报错啊,刚试过了,会打印1
- how can i HOT 1
- Hosting of Blog Issue
- 全局对象
- 为啥每次都要创建一个 Child函数来new 子类?现在不都是 const p1 = new Person(); const p2 = new Person()吗
- 文档内容中文件结构的错位 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blog.