1

Is it possible, while creating an instance of an object, to check during the instantiation itself the type of the parameter passed to the constructor, and set one of the object's variable accordingly? This is what I'm trying to do:

function Test(num1) {
    this.num1 = num1;

    this.isValidNumber = (function() { 
        console.log(this.num1); // logs "undefined" upon instantiation
        if (isNaN(this.num1)) {
            return false;
        } else {
            return true;
        }
    }());
}

var testObj = new Test(5);

I want isValidNumber's value to be true or false according to the parameter passed to the constructor.

2
  • this isn't what you think it is in the inner function. The inner function can reference the outer num1 function argument directly, or you can set this appropriately when calling the inner function by using .bind(), .call() or .apply(). Note also you can simplify (remove) the if/else block by just saying return !isNaN(...); Commented Jan 22, 2015 at 9:46
  • functions used in an immediately invoked function expression are scoped globally. this is window and has no property num1. Commented Jan 22, 2015 at 9:55

4 Answers 4

3

Your IIFE (Immediately-Invoked Function Expression) creates a new context, which means this doesn't point to your constructor anymore but to the Window object (unless you're using strict mode which would make it undefined). One possible solution is to execute this function with the right context:

this.isValidNumber = function() {
    if (isNaN(this.num1)) {
        return false;
    } else {
        return true;
    }
}.call(this);
Sign up to request clarification or add additional context in comments.

2 Comments

+1 because this is the correct solution, but it would be good if you explain why this works, instead of just being a code dump.
Sorry, I'm new to Stack Overflow and not used to the right way of doing things yet :) Edited the answer, thanks!
2

The much simpler solution is obviously:

function Test(num1) {
    this.num1 = num1;
    this.isValidNumber = !isNaN(this.num1);
}

5 Comments

What if this is just a simplified version of the OP's code?
You would want !isNaN(this.num1) here
@Scimonster I'm just pointing out the actual best solution for this particular example. It's entirely possible the OP is lost in the weeds here and doesn't see the forest for the trees. And that was a lot of proverbs in one sentence.
You're right deceze, I was lost, your solution is the simplest, thanks. I did learn something about the scope of 'this' from the answers here, thanks.
@Derpanel - It is also the most correct in my opinion. It is important to keep in mind that the code inside of the function will execute when called. This could even be broken out into your original if statement. if (isNaN(this.num1)) { this.isValidNumber = false; } else { this.isValidNumber = true; }
0

First of all, the code could (and probably should) be rewritten the following way:

function Test(num1) {
    this.num1 = num1;

    console.log(this.num1);
    this.isValidNumber = !isNaN(this.num1);
}

As for the reason why your code is not working as expected – your self invoking function has its own this parameter which is unrelated to the this parameter within your constructor.
In this case the this parameter of your self invoking function references the global object. (In strict mode it would've been undefined)

If you really need to reference the this parameter of a containing function then there are several ways to achieve this, the simplest of which being:

function Test(num1) {
    this.num1 = num1;

    var self = this;
    this.isValidNumber = (function() { 
        console.log(self.num1);
        return !isNaN(self.num1));
    }());
}

In your particular case you don't even need to capture it, and this would also achieve the same effect:

function Test(num1) {
    this.num1 = num1;

    this.isValidNumber = (function() { 
        console.log(num1);
        return !isNaN(num1));
    }());
}

But again, the self invoking function is simply not required here.

1 Comment

Your answer pretty much summed up everything I needed to know, thank you!
0

This in the inner function no longer references the parent object. A common way of storing the object reference is to create a 'self' variable and assign this to it when it's in the correct scope

function Test(num1) {
    var self = this;
    this.num1 = num1;

    this.isValidNumber = (function() { 
        console.log(self.num1); 
        if (isNaN(self.num1)) {
            return false;
        } else {
            return true;
        }
    }());
}

var testObj = new Test(5);

1 Comment

this doesn't refer to either function. In the outer function this refers to the newly created object; in the inner function this will be window (assuming running in a browser) or undefined depending on whether the code is in strict mode.

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.