gpt4 book ai didi

javascript - 如何从键 :function the same way as key:value in JavaScript? 检索值

转载 作者:行者123 更新时间:2023-12-02 18:56:50 25 4
gpt4 key购买 nike

如何以与检索 foo1.x 相同的方式检索 foo2.x?

var foo1 = {x: 4};
var foo2 = {x: function(){return addOne(foo1.x);}}

function addOne(var1){return (var1 + 1);}

alert(foo1.x);
alert(foo2.x()); // needs parentheses, but argument is never needed

最佳答案

看来你想要一个 getter function :

var foo1 = {x: 4},
foo2 = {get x() { return foo1.x + 1; }};

foo2.x; // 5
foo1.x = 0;
foo2.x; // 1

关于javascript - 如何从键 :function the same way as key:value in JavaScript? 检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15255493/

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