Javascript method chain

This working code uses 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 made it work. When I try to restore this without an SC class, it gives me:

TypeError: Object function () {
        return this.get('firstName') + " " + this.get('lastName');
    } has no method 'property'

What does the code look like to make it work?

+3
source share
2 answers

Sproutcore extends the prototype function.

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

Using sproutcore custom code 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 use their own mixin method, but the concept is the same: prototype extension

+3
source

, Sproutcode Function.prototype, property.

.

+1

Source: https://habr.com/ru/post/1773232/


All Articles