What should a JavaScript constructor return if it fails?

前端 未结 4 1854
醉酒成梦
醉酒成梦 2021-02-03 18:33

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 nul

相关标签:
4条回答
  • 2021-02-03 18:44

    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.)

    0 讨论(0)
  • 2021-02-03 18:51

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-03 19:04

    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.

    0 讨论(0)
提交回复
热议问题