基本Sproutcore:类方法,类变量帮助 [英] Basic Sproutcore: class method, class variables help

查看:158
本文介绍了基本Sproutcore:类方法,类变量帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is how i am defining a simple class with instance variables and instance methods.

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'

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

Could anyone help me out with a similar example of class variable (or similar behavoir), and class method?

谢谢

推荐答案

类方法/属性的完成方式如下:

A class Method/Property would be done like:

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

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

然后您可以按以下方式访问它:

Then you can access it like:

ExampleClass.classFoo

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

But don't forget that when accessing a property (or computed property) on an instance, that you need to use .get() like:

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:类方法,类变量帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆