admin管理员组

文章数量:1287183

I'm trying to implement a sort of Constructor Pattern into a library for a project. But, I want to check for certain conditions being passed before returning an object. If those conditions aren't met, then I want to stop the constructor and return false.

But, I noticed that no matter what I set as the return value, an object is always returned!

Even if I do this:

new function () { return false; }

The result is still an Object object:

In Chrome:

In Firefox:

Is there any way to have a constructor fail in Javascript?

I'm trying to implement a sort of Constructor Pattern into a library for a project. But, I want to check for certain conditions being passed before returning an object. If those conditions aren't met, then I want to stop the constructor and return false.

But, I noticed that no matter what I set as the return value, an object is always returned!

Even if I do this:

new function () { return false; }

The result is still an Object object:

In Chrome:

In Firefox:

Is there any way to have a constructor fail in Javascript?

Share Improve this question asked Mar 4, 2014 at 19:09 xyhhxxyhhx 6,6746 gold badges44 silver badges67 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 9

If those conditions aren't met, then I want to stop the constructor and return false.

You can't. The return value of a constructor function is ignored unless it's a non-null object. Any primitive value (or null) is just ignored by the new expression.

If you really want to have a flag value you return, you can do that, but it has to be an object. (But it's a bad idea, more below.) E.g.:

// The constructor
function Foo(num) {
    if (num < 27) {
        return Foo.BAD_ARG; // This is a bad idea, see the right way below
    }
    this.num = num;
}
Foo.BAD_ARG = {}; // Our special "bad argument" object

Is there any way to have a constructor fail in Javascript?

Yes: Throw an exception:

function Foo(num) {
    if (num < 27) {
        throw "`num` must be >= 27";
    }
    this.num = num;
}

Only way to return a non-object will be to wrap your constructor in a function and have it call (or not call) using new for you.

To do this, new on the outer function will need to be prohibited.


You can even use the same function, as long as you never call it with new.

function Foo() {
   if (!(this instanceof Foo)) {
       if (some_condition)
           return false;
       else
           return new Foo()
   }
   // your constructor code here
}

var x = Foo();

The danger is if you forget and accidentally use new. To remedy this, you can use a separate function.

function Foo() {
    // constructor code
}

function Bar() {
    if (this instanceof Bar)
        throw "Not a constructor"

    if (some_condition)
         return false;
    else
        return new Foo();
}

Throw an error out of the stack, e.g.

function thing(){
    throw new Error('nope');
}

try {
    var mine = new thing();
}
catch( e ){
    mine = null;
}

// mine is null

You could also do a sort of factory method, like:

createThing = function(){
   var obj = new thing();
   if( obj.feelsOK ){
       return obj;
   }
}

mine = createThing();
// mine is null

本文标签: javascriptTo Stop a ConstructorStack Overflow