Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7773163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:04:03+00:00 2026-06-01T17:04:03+00:00

I have a Foo constructor like this: function Foo(params) { this.x = params.x +

  • 0

I have a Foo constructor like this:

function Foo(params) {
  this.x = params.x + 5;
  ...  
}

Foo is called like this:

var foo = new Foo(someObject);

I would like to check the contents of params in Foo‘s constructor, and if something is wrong (e.g. params.x is not a number, or params.y is undefined), I would like foo to be null (which will mean that the Foo object wasn’t created).

One option, I guess, is to throw an exception in the constructor and wrap the call for new Foo(someObject) with try..catch (or create a function like makeFoo that does so).

How would you suggest to handle this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:04:05+00:00Added an answer on June 1, 2026 at 5:04 pm

    I would throw the exception. An invalid input is an exceptional condition, throwing an exception makes sense. I wouldn’t bother with the makeFoo wrapper; the calling code should handle it.


    Re

    I would like foo to be null (which will mean that the Foo object wasn’t created).

    There’s no way to make the expression new Foo(...) return null. Normally, the result of the new expression is a reference to the object that was constructed by the new operator and passed into the constructor function as this. The constructor function can override that and return a different object, but it can’t do so by returning null. If the constructor function doesn’t return anything, or returns something that isn’t an object (not including null; null isn’t an object [it has its own type, Null], despite the fact that typeof null is "object"), the result of the new expression will be the object created by the new operator. More in Sections 11.2.2 and 13.2.2.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes, Foo and Bar, that have constructors like this: class Foo
I have a class with a std::function constructor parameter. class ClazzA{ public: ClazzA(function<void()> foo){}
I'd like to bind operator new (see example below). If the constructor doesn't have
Say I have a Javascript class defined and instantiated like this: Demo = function()
I would like to return a noncopyable object of type Foo from a function.
We have php 5.3.0 and I would like to use the constructor _construct. But
I have a final member data: public final Foo foo; in the constructor, foo
Suppose I have: Foo foo; is there a shorthand for this? foo.operator->().operator()(1, 2);
I have class Foo(): function bar(): pass function foobar(): pass Rather than executing each
I have: var foo = '(bar)' foo.replace('(', '').replace(')', '') So, I get bar without

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.