Derpanel
Derpanel

Reputation: 329

Using Constructor variables during the instantiation

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.

Upvotes: 1

Views: 66

Answers (4)

Acidic
Acidic

Reputation: 6282

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.

Upvotes: 0

Benjamin De Cock
Benjamin De Cock

Reputation: 230

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

Upvotes: 3

JamesT
JamesT

Reputation: 3028

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

Upvotes: 0

deceze
deceze

Reputation: 522081

The much simpler solution is obviously:

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

Upvotes: 2

Related Questions