gpt4 book ai didi

sproutcore - 基本Sproutcore : class method,类变量帮助

转载 作者:行者123 更新时间:2023-12-03 13:38:47 25 4
gpt4 key购买 nike

这就是我定义带有实例变量和实例方法的简单类的方式。

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'

有人可以通过类变量(或类似行为)和类方法的类似示例帮助我吗?

谢谢

最佳答案

类方法/属性将像这样完成:

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';

关于sproutcore - 基本Sproutcore : class method,类变量帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5103577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com