简体   繁体   English

如何在CoffeeScript中访问类方法?

[英]How to access a class method in CoffeeScript?

I am prefacing this: I am new to CoffeeScript and JavaScript prototyping as a whole. 我为此作序:整体来说,我是CoffeeScript和JavaScript原型的新手。

That being said, I am trying to create a new Object of a class in CoffeeScript, and then call a getter to retrieve the variable but what happens is I get an 'undefined' response instead. 话虽这么说,我正在尝试在CoffeeScript中创建类的新Object,然后调用getter来检索变量,但是发生的是我得到了“未定义”响应。 What am I doing wrong, or how I should I approach this. 我在做什么错,或者我应该如何处理。 I am fairly new and trying to follow the KISS standard. 我还很新,正在尝试遵循KISS标准。

class TestHandler
  constructor: ->
  testArray = []
  getTestArray: ->
    @testArray

tH = new TestHandler()
tH.testArray.push 1 #testArray returns undefined
tH.getTestArray().push 1 #getTestArray returns undefined
console.log tH.getTestArray()

When you create a class in CoffeeScript every metod you list down there goes to the prototype property (in this case TestHandler.prototype ) and therefore is shared among instances of that class, however every other property, will be either a member of TestHandler itself (if name prepended with @ ) or a private variable of scoped to a function that class will eventually be compiled to (like was with your testArray = [] ). 当您在CoffeeScript中创建一个类时,您列出的每个方法都会进入prototype属性(在本例中为TestHandler.prototype ),因此在该类的实例之间共享,但是其他所有属性都将是TestHandler本身的成员(如果名称前面带有@ )或范围限定于class最终将被编译到的函数的私有变量(例如与testArray = [] )。 It means, that your tH won't have a property called testArray . 这意味着,您的tH将没有名为testArray的属性。 If you wanted it to be an own property of the each instance, put this into the constructor: 如果希望它成为每个实例的自己的属性,请将其放入构造函数中:

class TestHandler
    constructor: ->
        @testArray = []
    getTestArray: ->
        @testArray

tH = new TestHandler()
tH.testArray.push 1 #now everything works
tH.getTestArray().push 1
console.log tH.getTestArray()

Generally, where the property will land depends on how you write in the class declaration. 通常,该属性将到达的位置取决于您在class声明中的编写方式。 Look at this CS code 看这个CS代码

 class TestHandler
   constructor: ->
     @testArray = [] #1
   testArray = [] #2
   @testArray = [] #3
   getTestArray: -> #4
   testArray: [] #5

and the code that it compiles to: 及其编译的代码:

 var TestHandler;

 TestHandler = (function() {
   var testArray;

   function TestHandler() {
     #1 @thisArray in constructor becomes instance property accessor
     this.testArray = [];
   }

   #2 becomes a local variable, not available outside this function
   testArray = [];

   #3 becomes a "static" property
   TestHandler.testArray = [];

   #4 is a method that goes to the prototype, so that it can be shared with all instances
   TestHandler.prototype.getTestArray = function() {};

   #5 is a property that goes to the prototype and can be shared with all instances (although this shouldn't be done unless you know what you do)
   TestHandler.prototype.testArray = [];

   return TestHandler;

 })();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM