DEV Community

RadiumAg
RadiumAg

Posted on

Why does the code in Nodejs and Bowers run differently

I'm learning the combination inheritance of JavaScript, but I found the code in Nodejs and Bowers run differently。

(() => {
  function Supertype (name) {
    this.name = name;
    this.colors = ['red', 'blue', 'green'];
  }

  Supertype.prototype.sayName = function () {
    console.log(this);
  };

  function SubType (name, age) {
    Supertype.call(this, name);
    this.age = age;
  }

  SubType.prototype = new Supertype();
  SubType.prototype.sayAge = function () {
    console.log(this);
  };

  const instance1 = new SubType('Nicholas', 29);
  instance1.colors.push('black');
  console.log(instance1.colors);
  instance1.sayName();
  instance1.sayAge();
})();

Enter fullscreen mode Exit fullscreen mode

In Bowers(Edge of the last version)

Image description

In Nodejs(17.1.0)

Image description

Of course, the result is the same, but I wonder why the name of this is different, it confused me。

Top comments (0)