JavaScript 中的方法链
这个工作代码正在使用 Sproutcore:
person = SC.Object.create({
firstName: 'Foo',
lastName: 'Bar',
fullName: function() {
return this.get('firstName') + " " + this.get('lastName');
}.property()
});
console.log(person.get('fullName')); // "Foo Bar"
我想知道 property() 在哪里声明以及他们如何使其工作。 当我尝试在没有 SC 类的情况下重建它时,它给了我:
TypeError: Object function () {
return this.get('firstName') + " " + this.get('lastName');
} has no method 'property'
代码看起来如何使其工作?
This working code is using Sproutcore:
person = SC.Object.create({
firstName: 'Foo',
lastName: 'Bar',
fullName: function() {
return this.get('firstName') + " " + this.get('lastName');
}.property()
});
console.log(person.get('fullName')); // "Foo Bar"
I wonder where property() is declared and how they have made this to work.
When I try to reconstruct this without the SC class, it gives me:
TypeError: Object function () {
return this.get('firstName') + " " + this.get('lastName');
} has no method 'property'
How does the code looks like to make it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Sproutcore正在扩展函数原型。
sproutcore具体使用的代码位于 https://github .com/sproutcore/sproutcore/blob/master/frameworks/runtime/core.js#L908
在他们的例子中,他们使用自己的 mixin 方法,但概念是相同的:扩展原型
Sproutcore is extending the function prototype.
The specific code use by sproutcore is at https://github.com/sproutcore/sproutcore/blob/master/frameworks/runtime/core.js#L908
In their case, they are using their own mixin method, but the concept is the same: extending the prototype
据推测,Sproutcode 已修改
Function.prototype
以包含property
函数。您可以直接查看源代码。
Presumably, Sproutcode has modified
Function.prototype
to include aproperty
function.You could just look at the source code.