简体   繁体   English

JavaScript:构造函数与原型

[英]JavaScript: Constructor vs Prototype

This has been answered before, but I wanted to confirm my understanding. 这已经得到了回答,但我想证实我的理解。 In this code: 在这段代码中:

var somePrototype = {
  speak: function() {
    console.log("I was made with a prototype");
  }
}

function someConstructor() {
  this.speak = function() {
    console.log("I was made with a constructor");
  }
}

var obj1 = Object.create(somePrototype);
var obj2 = new someConstructor();

obj1.speak();
obj2.speak();

They are both fundamentally doing the same thing, correct? 他们从根本上做同样的事情,对吗? The only difference is that the function someConstructor() is hoisted, meaning I can call new instances of it before it is defined, if needed, while the var somePrototype can only be called after it's been defined. 唯一的区别是function someConstructor()被提升,这意味着我可以在定义之前调用它的新实例(如果需要),而var somePrototype只能在定义之后调用。 Other than that, there's no difference? 除此之外,没有区别?

The differences between 2 approaches (using Object.create() and constructor invocation) are: 两种方法(使用Object.create()和构造函数调用)之间的差异是:

The creation: 创作:

  • Object.create(somePrototype) creates a new object making the somePrototype it's prototype; Object.create(somePrototype)创建一个新对象,使somePrototype成为原型;
  • new someConstructor() creates an object using constructor invocation. new someConstructor()使用构造函数调用创建一个对象。 The prototype of the obj2 is a simple object: new Object() obj2的原型是一个简单的对象: new Object()

The properties inheritance: 属性继承:

  • obj1 inherits the property speak , which is a function. obj1继承属性speak ,这是一个函数。 If this property changes in the somePrototype object, this will affect any objects created with Object.create(somePrototype) which inherit it. 如果somePrototype对象中的此属性发生更改,则会影响使用继承它的Object.create(somePrototype)创建的任何对象。
    Object.keys(obj1) will return [] , because the object has no own properties. Object.keys(obj1)将返回[] ,因为该对象没有自己的属性。
  • obj2 contains an own property speak . obj2包含自己的属性speak Modifying this property on a single instance won't affect any other instances created using new someConstructor() . 在单个实例上修改此属性不会影响使用new someConstructor()创建的任何其他实例。
    Object.keys(obj2) will return ['speak'] as its listed property. Object.keys(obj2)将返回['speak']作为其列出的属性。

The constructor: 构造函数:

  • obj1.constructor === Object is true obj1.constructor === Objecttrue
  • obj2.constructor === someConstructor is true obj2.constructor === someConstructortrue

Hoisting: 吊装:

  • someConstructor is hoisted to the top of scope it was created. someConstructor被提升到它创建的范围的顶部。 So it can be used before the function declaration. 所以它可以在函数声明之前使用。
  • And sure somePrototype is not hoisted with the object literal, so should be used after setting up the value. 并确保somePrototype不会与对象文字一起提升,因此应在设置值后使用。

Check this interesting post about constructor property. 查看有关constructor属性的这篇有趣帖子

The Object.create() call creates an object and gives it the prototype you requested. Object.create()调用创建一个对象并为其提供您请求的原型。 The new call creates an object that's directly decorated by that constructor function. new调用创建一个由该构造函数直接修饰的对象。

The difference is that the object created by the constructor has an own property whose value is that function with the console.log() . 不同之处在于构造函数创建的对象具有自己的属性,其值是使用console.log()函数。 The Object.create() call creates an object that inherits a similar function from the prototype object. Object.create()调用创建一个从原型对象继承类似函数的对象。

If you passed the first object to Object.keys() , you wouldn't see the "speak" property; 如果将第一个对象传递给Object.keys() ,则不会看到“speak”属性; if you passed the second object, you would. 如果你传递了第二个对象,你会的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM