gpt4 book ai didi

javascript - 如何将对象值传递给其属性对象?

转载 作者:行者123 更新时间:2023-12-03 02:01:09 25 4
gpt4 key购买 nike

如何将对象值传递到其属性对象?

我尝试过这样

var obj = {
a:2,
b:this.a
}

然后尝试obj.b,它给出未定义..我可以在对象中创建函数吗?

预期输出2

最佳答案

const obj = {
a: 2,
get b() {
return this.a;
}
};

console.log(obj.b);

const obj2 = {
a: 2,
b: function() {
return this.a;
}
};

console.log(obj2.b());

查看实际效果:https://jsfiddle.net/rqnbxw86/

关于javascript - 如何将对象值传递给其属性对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50019733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com