JavaScript 中的方法链

发布于 2024-09-30 10:57:24 字数 538 浏览 5 评论 0原文

这个工作代码正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

书信已泛黄 2024-10-07 10:57:24

Sproutcore正在扩展函数原型。

Function.prototype.property = function() { /* code here */ };

sproutcore具体使用的代码位于 https://github .com/sproutcore/sproutcore/blob/master/frameworks/runtime/core.js#L908

SC.mixin(Function.prototype, 
//...snip...

property: function() {
    this.dependentKeys = SC.$A(arguments) ;
    var guid = SC.guidFor(this) ;
    this.cacheKey = "__cache__" + guid ;
    this.lastSetValueKey = "__lastValue__" + guid ;
    this.isProperty = YES ;
    return this ;
  },
//snip
);

在他们的例子中,他们使用自己的 mixin 方法,但概念是相同的:扩展原型

Sproutcore is extending the function prototype.

Function.prototype.property = function() { /* code here */ };

The specific code use by sproutcore is at https://github.com/sproutcore/sproutcore/blob/master/frameworks/runtime/core.js#L908

SC.mixin(Function.prototype, 
//...snip...

property: function() {
    this.dependentKeys = SC.$A(arguments) ;
    var guid = SC.guidFor(this) ;
    this.cacheKey = "__cache__" + guid ;
    this.lastSetValueKey = "__lastValue__" + guid ;
    this.isProperty = YES ;
    return this ;
  },
//snip
);

In their case, they are using their own mixin method, but the concept is the same: extending the prototype

几度春秋 2024-10-07 10:57:24

据推测,Sproutcode 已修改 Function.prototype 以包含 property 函数。

您可以直接查看源代码

Presumably, Sproutcode has modified Function.prototype to include a property function.

You could just look at the source code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文