我正在学习 Aurelia 教程,但有意只使用 ES5 和 AMD/RequireJS,因为最初我试图减少我可能需要在我当前的生产应用程序中引入的技术过载(目前正在使用 Angular 但我我正在考虑换用 Aurelia 而不是 Angular 2),但我一直坚持让计算属性正常工作。
我注意到 update made in April从允许以下语法的 Object 原型(prototype)中删除了计算函数,但我不确定我应该做什么而不是以下语法:
Welcome.computed({
fullName : function() { return this.firstName + " " + this.lastName; }
});
我可以通过以下实现相同的效果,但考虑到正在实现的目标,它似乎相当冗长!有没有正确的 Aurelia 方法?
Object.defineProperty(
Welcome.prototype,
"fullName",
{
get: function() { return this.firstName + " " + this.lastName },
enumerable: true
});
最佳答案
您所做的是正确的 - 您总是可以重新添加计算工厂方法,因为编写所有这些额外的 ES5 代码可能会变得乏味。
我还建议指定计算属性的依赖关系以优化数据绑定(bind)效率。以下代码片段是等效的:
ES5:
function fullName() { return this.firstName + ' ' + this.lastName }
fullName.dependencies = ['firstName', 'lastName'];
Object.defineProperty(
Welcome.prototype,
'fullName',
{
get: fullName,
enumerable: true
});
ES6/ES7:
@computedFrom('firstName', 'lastName')
get fullName() {
return `${this.firstName} ${this.lastName}`;
}
关于javascript - Aurelia 使用 ES5 计算属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32028553/