0

For a function returning a new object, it could be used as a factory method, or a constructor function, to create new object, like below:

function Person()
{
    var o=new Object();
    o.age=30;
    return o;
}
var p1=Person();
var p2=new Person();
console.log(typeof(p1)==typeof(p2))

I wonder what's the core difference between p1 and p2 -------- p1 is created directly from the function, p2 is created by the constructor symantics. So p1 and p2 have identical structure, or they have some suttle differences?

Or, is there any difference between objects that are created using "new" or without "new"?

Troskyvs
  • 7,537
  • 7
  • 47
  • 115
  • I don't think it's duplicated, I'm asking about the difference between p1 and p2, they're created by different semantics. – Troskyvs Aug 15 '16 at 03:27
  • I don't think the duplication is wrong, but I don't think it really answers the question. Really, you need to understand the fundamental usefulness of the `new` keyword. This might help: http://stackoverflow.com/questions/1646698/what-is-the-new-keyword-in-javascript – Dandy Aug 15 '16 at 04:44

0 Answers0