gpt4 book ai didi

javascript - 函数类正确使用的property()方法

转载 作者:行者123 更新时间:2023-11-29 19:44:33 29 4
gpt4 key购买 nike

文档中的示例:

App.Person = Ember.Object.extend({
// these will be supplied by `create`
firstName: null,
lastName: null,

fullName: function() {
return this.get('firstName') + ' ' + this.get('lastName');
}.property('firstName', 'lastName')
});

var ironMan = App.Person.create({
firstName: "Tony",
lastName: "Stark"
});

ironMan.get('fullName') // "Tony Stark"

我不明白为什么我必须指定像 .property('firstName', 'lastName') 这样的依赖项,如果我已经指出需要返回什么:return this.get(' firstName') + ' ' + this.get('lastName') 上面的示例可以在没有它的情况下工作(只需使用 property())。谢谢。

最佳答案

用于缓存。

来自 docs :

By default the function backing the computed property will only be called once and the result will be cached. You can specify various properties that your computed property is dependent on. This will force the cached result to be recomputed if the dependencies are modified.

关于javascript - 函数类正确使用的property()方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20687691/

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