1

I have a function for creating objects as follows:

function person() {
    this.name = "test person";
}
var me = new person();

Now I'm planning to wrap this function into another one like this for assertion purposes.

function reliable_person() {
    /* do check on params if any*/
    return new person();
}
var me = new reliable_person();

Is this a possible approach? I know there are better ways for such checks within the constructor itself, but is this a reliable solution?

4
  • 1
    What do you mean by "reliable"? Yes, it'll work, but the new in new reliable_person is entirely superfluous. reliable_person is just a factory function, not an object constructor. Commented Jun 15, 2016 at 9:01
  • According to ECMAScript, new operator discards the object it created if the function constructor returns an object. In this case yes it is superfluous. @deceze Commented Jun 15, 2016 at 9:10
  • At second snippet reliable_person function behaves like a factory pattern so no call with new keyword is required when an object is created through reliable_person() function. Commented Jun 15, 2016 at 9:14
  • This answer shows the procedure for creating a factory function. I think this would solve your problem. Commented Jun 15, 2016 at 9:19

1 Answer 1

2

Invoking a function with new constructs a new object and uses the given function to initialise the object. Something that's a bit special about this is that if you return a non-primitive value, like an object, from that function, that value will be used as the new object instead. Sounds complicated, but what it boils down to is that reliable_person() and new reliable_person() result in the exact same thing, because you're returning an object from it. So using new with that function is pointless.

Removing the superfluous new from it, all that's left is a normal function which returns an object (a "factory function"). Yes, that works and is "reliable".

Sign up to request clarification or add additional context in comments.

3 Comments

The reason why I wanted to go with this approach is that, I don't want to disturb the object creation interface (new) for the end user using the function I plan to make.
Callers would be confused, since they'll receive an object of type person instead of reliable_person. There's no need to dress something up without purpose. An object is fine, a factory function is fine. Be clear about what each does and what they're used for, period.
Just as a side note, if you want to make reliable_person a constructor, inside reliable_person, run person.call(this), and after the definition of the function, set reliable_person's prototype to person's prototype, with reliable_person.prototype = Object.create(person.prototype). Then, reliable_person will inherit from person and you'll be able to get a new reliable_person()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.