基本Sproutcore:类方法,类变量帮助

时间:2011-02-24 10:49:26

标签: sproutcore

这是我用实例变量和实例方法定义一个简单类的方法。

ExampleClass = SC.Object.extend({
    foo:undefined,
    bar: function() {
        this.foo = "Hello world";
        console.log( this.foo );
    }
}

// test
var testInstance = ExampleClass.create();
testInstance.bar();    // outputs 'Hello world'

有没有人可以帮我解决类变量(或类似行为)和类方法的类似例子?

由于

1 个答案:

答案 0 :(得分:0)

类Method / Property将完成如下:

ExampleClass = SC.Object.extend({
  foo:undefined,
  bar: function() {
    this.foo = "Hello world";
    console.log( this.foo );
  }
}

ExampleClass.mixin({
  classFoo: "foo",
  classBar: function() {
    return "Bar";
  }
})

然后您可以像访问它一样访问它:

ExampleClass.classFoo

但是不要忘记在访问实例上的属性(或计算属性)时,您需要使用.get(),如:

var example = ExampleClass.create();
// Good
example.get('foo');
example.set('foo', 'baz');

// BAD!! Don't do this, or Bindings/ Observes won't work.
example.foo; 
example.foo = 'baz';