Typically, in a constructor function, the object that is bound to this within the function is returned by it when it is called with the new prefix. But I would imagine that it’s also possible (I think I even read this in crockford’s book) to manually return your own value. Are there any places where such a practice is useful?
Typically, in a constructor function, the object that is bound to this within the
Share
If you return a value type from the constructor, you’ll get different behavior depending on if
newwas used. That’s howStringworks. Look at this object in your JavaScript console:Little
scontains a string value, but bigScontains a string Object. A subtle difference, perhaps.You could go way beyond that and use the same function for different purposes:
Depending on whether
newwas used, you’d get back something completely different.