3

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?

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

You could just look at the source code.

3 Comments

Yeah im new to programming. How am I supposed to know where the code for this resides?
@weng: google.com, or just look at the SproutCore web site (also found through Google) sproutcore.com/documentation
@weng: oh! sorry. Well, that's what github search is for. Unfortunately, it seems to be down right now: github.com/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.