admin管理员组文章数量:1297114
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. Other than that, there's no difference?
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. Other than that, there's no difference?
- They're doing similar things, but they're quite distinctly different. – Pointy Commented Apr 8, 2016 at 4:48
- there are several (maybe subtle) differences, but i can't help but think it's been explained before... – dandavis Commented Apr 8, 2016 at 4:52
- @dandavis yes, it's been explained before :) However, the semantics of JavaScript and prototypes are weird and confusing. – Pointy Commented Apr 8, 2016 at 4:52
-
1
@SterlingArcher well it sure is a prototype when it's passed in to
Object.create()
. – Pointy Commented Apr 8, 2016 at 4:53 - 1 @Pointy—ah, language nuances! If an object is assigned to a function's prototype property, is it a prototype if the function is never used as a constructor? ;-) – RobG Commented Apr 8, 2016 at 5:19
2 Answers
Reset to default 8The differences between 2 approaches (using Object.create()
and constructor invocation) are:
The creation:
Object.create(somePrototype)
creates a new object making thesomePrototype
it's prototype;new someConstructor()
creates an object using constructor invocation. The prototype of theobj2
is a simple object:new Object()
The properties inheritance:
obj1
inherits the propertyspeak
, which is a function. If this property changes in thesomePrototype
object, this will affect any objects created withObject.create(somePrototype)
which inherit it.
Object.keys(obj1)
will return[]
, because the object has no own properties.obj2
contains an own propertyspeak
. Modifying this property on a single instance won't affect any other instances created usingnew someConstructor()
.
Object.keys(obj2)
will return['speak']
as its listed property.
The constructor:
obj1.constructor === Object
istrue
obj2.constructor === someConstructor
istrue
Hoisting:
someConstructor
is hoisted to the top of scope it was created. 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.
Check this interesting post about constructor
property.
The Object.create()
call creates an object and gives it the prototype you requested. The new
call creates an object that's directly decorated by that constructor function.
The difference is that the object created by the constructor has an own property whose value is that function with the console.log()
. The Object.create()
call creates an object that inherits a similar function from the prototype object.
If you passed the first object to Object.keys()
, you wouldn't see the "speak" property; if you passed the second object, you would.
本文标签: JavaScript Constructor vs PrototypeStack Overflow
版权声明:本文标题:JavaScript: Constructor vs Prototype - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741646690a2390224.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论