Giter VIP home page Giter VIP logo

Comments (1)

mqyqingfeng avatar mqyqingfeng commented on May 2, 2024

我们以这张图为例:
原型关系图

准确的说,实例的 __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 的时候,步骤如下:

  1. 创建新对象
  2. 设置该对象的原型对象为构造函数的 prototype
  3. 将 this 指向新对象,这一步使用了 apply,表明函数此时会被执行一遍
  4. 返回新对象

我们再看文中的这个例子:

当执行 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.