1

I am currently reading Kyle Simpson's You Don't know JS, trying to understand the whole prototype pattern.

It says we can achieve prototypal inheritance between Foo and Bar as follows:

      function Foo(name) {
        this.name = name;
      }
      
      Foo.prototype.myName = function () {
        return this.name;
      };
      
      function Bar(name, label) {
        Foo.call(this, name);
        this.label = label;
      }
      
      // here, we make a new `Bar.prototype`
      // linked to `Foo.prototype`
      Bar.prototype = Object.create(Foo.prototype);
      
      // Beware! Now `Bar.prototype.constructor` is gone,
      // and might need to be manually "fixed" if you're
      // in the habit of relying on such properties!
      
      Bar.prototype.myLabel = function () {
        return this.label;
      };
      
      var a = new Bar("a", "obj a");
      
      console.log(a.myName()); // "a"
      console.log(a.myLabel()); // "obj a"

I understand the link is made in the line

Bar.prototype = Object.create(Foo.prototype);

so that Bar's prototype points to an object whose prototype is Foo.prototype.

I was wondering why don't we just do this:

Bar.prototype = Object.assign({}, Foo.prototype);

We achieve the same result and now we have one level of prototype chain lookup for all methods instead of two.

5
  • How do you figure that the prototype chain is shorter with Object.assign? Commented Apr 26, 2018 at 20:05
  • @ScottMarcus you can run the snippet with Object.assign 😉 Commented Apr 26, 2018 at 20:09
  • It might be one level lower, but it's not the same object - changing stuff in Foo.prototype will not be reflected on your assigned prototype. Commented Apr 26, 2018 at 20:19
  • @destoryer It's not, I agree. But I am saying that it produces the exact same effect of inheritance. Commented Apr 26, 2018 at 20:21
  • If you are to add a function on Foo.prototype, you would not be able to use it. Commented Apr 26, 2018 at 20:22

1 Answer 1

1

We achieve the same result

No, we don't. Bar.prototype would then not inherit from Foo.prototype, instead it would have its own properties. Sure, the values from Foo.prototype would be copied over, but that's just a snapshot of Foo.prototype from the time Object.assign was called instead of a live connection.

Sign up to request clarification or add additional context in comments.

2 Comments

So, would you say it is better to use Object.assign when I am not changing for example, Foo at runtime? (which I think mostly would be the case if inheritance is my goal).
No, it would not be better. Copying the properties requires more memory and does not allow engines to optimise method calls like inheritance does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.