What should a JavaScript constructor return if it

2019-03-17 04:06发布

If I have a javascript class which cannot be instantiated what should the constructor return that I can test for. The constructor always returns an object so I cannot return null if the constructor fails.

function SomeClass(id) {
  if(typeof(id) === 'number' {
    // This is good
    this.id = id;
  } else {
    // This is bad
    // This return is ignored and an empty object is returned
    return null;
  }
}

var a = new SomeClass('badParam');
if(a){
  // is true even though the class expects a number.
}

// Could use this check
if(a.id !== undefined){
  // Do some stuff
}

but it seems there should be a better way.

4条回答
神经病院院长
2楼-- · 2019-03-17 04:17

It is probably best to throw an exception to notify the caller that the initialization failed and to take appropriate action.

Return codes are fine, but for the most part there is no motivation for the caller to implement the checks on the return code.

My advice is to break hard and break soon. This will make contract violations very evident during testing.

查看更多
放荡不羁爱自由
3楼-- · 2019-03-17 04:20

Returning any non-object from the constructor is practically the same as exiting the constructor. (The constructor will return a new object, with a prototype if one was specified.)

So, returning null, undefined, or 42 from the constructor are equivalent.

Check out section 13.2.2 of the ECMAScript spec (pdf) for more info.

查看更多
淡お忘
4楼-- · 2019-03-17 04:20

Use I sentinel. I like to

return {invalid:true};

This looks clean:

var x = new X();
if (x.invalid) { // ...

(There's no way to return a non-true value from a constructor, so you can't put new in the conditional as you might with another language.)

查看更多
Root(大扎)
5楼-- · 2019-03-17 04:23

My first approach:

  1. Don't allow a constructor to fail; consider alternatives

My second approach:

  1. If a constructor fails, it must only fail because of a programming error and thus;
  2. should throw an exception and;
  3. must not return 'a status code' (see Emmett's answer for why returning null doesn't work anyway)

I have never designed a constructor (something invoked as the target of new) to return anything except an object of the "expected" type.

Fail fast, avoid being too clever, and save time debugging hard-to-find bugs.

Happy coding.

查看更多
登录 后发表回答