StormShadow
StormShadow

Reputation: 1627

How do I assign the return value from a method to another object property in Javascript?

I'm converting some old code to object literal notation in Javascript, and I'm afraid I've hit a bit of a bugbear. I know how to define properties, and I know how to define methods, but what if I want to assign the return value from a method as a property?

I've supplied code with the error output from Chrome's console. I can't see what I'm doing wrong, but the console is telling me that I'm either trying to go to something nonexistent in the global scope, or just something nonexistent. Here it is:

Code:

var testobj = {
    a: 2,
    b: 4,
    c: function() {
        return this.a * this.b;
    },
    d: this.c(), // OK, got it, it's trying to call it from global scope. Fine.
    e: function() {
        if (this.d) {
            console.log("SUCCESS");
            console.log(this.d);
        } else {
            console.log("ERROR");
        }
    }
}

Error:

TypeError: Object [object global] has no method 'c'

New code:

var testobj = {
    a: 2,
    b: 4,
    c: function() {
        return this.a * this.b;
    },
    d: testobj.c(), // but if I change it like this, it still doesn't work. What gives?
    e: function() {
        if (this.d) {
            console.log("SUCCESS");
            console.log(this.d);
        } else {
            console.log("ERROR");
        }
    }
}

New error:

TypeError: Cannot call method 'c' of undefined

Can anyone see what I'm doing wrong?

Upvotes: 2

Views: 100

Answers (3)

Peter
Peter

Reputation: 14108

You can fix it by using:

var testobj = {
    a: 2,
    b: 4,
    c: function() {
        return this.a * this.b;
    },
    d: function() {
        return this.c();
    },
    e: function() {
        if (this.d) {
            console.log("SUCCESS");
            console.log(this.d);
        } else {
            console.log("ERROR");
        }
    }
}

When you do d: this.c(), this is actually the global object. This is because, at the time of creating your testobj, the scope is the global object, so this is the global object.

If you use

d: function() {
    return this.c();
}

you're just setting testobj.c to a certain function. The this inside that function is only evaluated when you call d. So when you do call d, it will check the scope and see that the scope is testobj. And as testobj has a c function, it wall call and return that.

I've put this in a jsFiddle to see it in action.

Upvotes: 3

Onur Topal
Onur Topal

Reputation: 3061

I believe you want to see the return value of the c in d.

In this cases I assign d after the object as it doesn t have any instance in var ob = {...} as it is still being created.

var testobj = {
    a: 2,
    b: 4,
    c: function() {
        return this.a * this.b;
    },
    e: function() {
        if (this.d) {
            console.log("SUCCESS");
            console.log(this.d);
        } else {
            console.log("ERROR");
        }
    }
}

testobj.d = testobj.c();

alert(testobj.d);
alert(testobj.c());

Upvotes: 0

Balint Bako
Balint Bako

Reputation: 2560

This works ((http://jsfiddle.net/balintbako/n6YjE/):

var testobj = {
    a: 2,
    b: 4,
    c: function () {
        alert(this.a + this.b);
    },
    d: function () {
        this.c();
    }
};

testobj.c();

Upvotes: 0

Related Questions