I am new to Javascript and now studying it...
var person = function() {
this.name = "name"
};
var person2 = function() {
var obj = {};
obj.name = "name";
return obj;
};
Let's suppose we have two functions shown above. It seems that objects can be created by using either of the functions. For example)
var p = new person();
var p2 = new person2();
My question is: What's difference between person vs person2? Are they exactly the same? If not which one is a more preferable way to use?
Thanks
new. There is a difference, which should become clearer when you read aboutnew: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/….new, which changes things.newwon't make a difference. (of course it makes a difference in so far thatthiswill refer to different values inside the function, but it doesn't make a difference for the result of the function call).p.constructorandp2.constructorare not the same.