如何将一个方法的返回值赋给Javascript中的另一个对象属性?

时间:2022-10-08 07:45:43

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?

在Javascript中,我将一些旧代码转换为对象的文字符号,恐怕我遇到了一点麻烦。我知道如何定义属性,我知道如何定义方法,但是如果我想将方法的返回值作为属性来分配呢?

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:

我提供了来自Chrome控制台的错误输出的代码。我看不出我做错了什么,但是控制台告诉我,我不是在尝试去寻找全局范围内的不存在的东西,或者只是一些不存在的东西。这里是:

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?

有人知道我做错了什么吗?

3 个解决方案

#1


3  

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.

当您执行d: this.c()时,这实际上是全局对象。这是因为在创建testobj时,范围是全局对象,所以这是全局对象。

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.

你只是设置testobj。c的某个函数。这个函数的内部只有当你调用d时才会被计算,所以当你调用d时,它会检查作用域并看到作用域是testobj。当testobj有一个c函数时,它会调用并返回它。

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

我把它放在一个jsFiddle中,以观察它的作用。

#2


0  

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

这是((http://jsfiddle.net/balintbako/n6YjE/):

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

testobj.c();

#3


0  

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

我相信你希望看到c在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.

在这种情况下,我在对象后面分配d,因为它没有任何实例在var ob ={…}因为它还在被创建。

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

#1


3  

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.

当您执行d: this.c()时,这实际上是全局对象。这是因为在创建testobj时,范围是全局对象,所以这是全局对象。

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.

你只是设置testobj。c的某个函数。这个函数的内部只有当你调用d时才会被计算,所以当你调用d时,它会检查作用域并看到作用域是testobj。当testobj有一个c函数时,它会调用并返回它。

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

我把它放在一个jsFiddle中,以观察它的作用。

#2


0  

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

这是((http://jsfiddle.net/balintbako/n6YjE/):

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

testobj.c();

#3


0  

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

我相信你希望看到c在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.

在这种情况下,我在对象后面分配d,因为它没有任何实例在var ob ={…}因为它还在被创建。

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