2014-02-07 35 views
0

大部分時間我只需要爲JavaScript添加一些動態的簡單HTML。然而,最近發現的CoffeeScript後,我感興趣的面向JavaScript的」 *對象,這裏是CoffeeScript的一些代碼。原型方法中的「this」並不總是指對象本身的原型?

class MyClass 
    constructor: (title, purpose)-> 
     @title = typeof title is undefined ? "My Class" : title 
     @purpose = typeof purpose is undefined ? "None" : purpose 
     @myMethod() 

    myMethod: -> 
     _getTitle = @getTitle 
     _getPurpose = @getPurpose 
     $(window).click -> 
      _getTitle() 
      _getPurpose() 
      return 
     return 

    getTitle: -> 
     _title = @title 
     window.console.log "Title of the class this object belongs to is: #{_title}" 
     return 

    getPurpose: -> 
     _purpose = @purpose 
     window.console.log "Purpose of creating this class is: #{_purpose}" 
     return 

    title = "" 
    purpose = "" 

myObject = new MyClass("Testbed", "to test Object Oriented JavaScript") 

對於那些喜歡誰的JavaScript,這裏是編譯(?)的JavaScript。

var MyClass, myObject; 

MyClass = (function() { 
    var purpose, title; 

    function MyClass(title, purpose) { 
    var _ref, _ref1; 
    this.title = (_ref = typeof title === void 0) != null ? _ref : { 
     "My Class": title 
    }; 
    this.purpose = (_ref1 = typeof purpose === void 0) != null ? _ref1 : { 
     "None": purpose 
    }; 
    this.myMethod(); 
    } 

    MyClass.prototype.myMethod = function() { 
    var _getPurpose, _getTitle; 
    _getTitle = this.getTitle; 
    _getPurpose = this.getPurpose; 
    $(window).click(function() { 
     _getTitle(); 
     _getPurpose(); 
    }); 
    }; 

    MyClass.prototype.getTitle = function() { 
    var _title; 
    _title = this.title; 
    window.console.log("Title of the class this object belongs to is: " + _title); 
    }; 

    MyClass.prototype.getPurpose = function() { 
    var _purpose; 
    _purpose = this.purpose; 
    window.console.log("Purpose of creating this class is: " + _purpose); 
    }; 

    title = ""; 

    purpose = ""; 

    return MyClass; 

})(); 

myObject = new MyClass("Testbed", "to test Object Oriented JavaScript"); 

很抱歉的長碼我必須儘量保持它很有趣的事情是,這段代碼的輸出:

Title of the class this object belongs to is: undefined 
Purpose of creating this class is: undefined

,而我也期待輸出:

Title of the class this object belongs to is: Testbed 
Purpose of creating this class is: to test Object Oriented JavaScript

我可以發誓,這是它的工作原理,當我最後修補它時(大約六個月前)。我瞭解到,在作爲原型的一部分的方法中,this指的是原型本身。而this.something實際上會指向object.something。而在此示例中,在myObject.myMethod()內,this表現得應該如此,this.getTitle()表示myObject.getTitle()。在myObject.getTitle()內部,但是,this是指window。爲什麼?

是否因爲getTitle()$(window).click()處理程序中被調用?但爲什麼會改變上下文? getTitle()仍然是myObject的財產。

此外,你看到我在這裏試圖完成。我怎麼能做到這一點?

回答

0

有幾個問題。

1)你永遠不會從.getPurpose.getTitle

2返回任何東西)你應該創建myMethod的這個參考。即var me = this,然後在事件監聽器內部呼叫me.getTitle()me.getPurpose()。這是因爲在事件監聽器(window onclick)內部,這是指窗口而不是對象。

3)它看起來像你的三元表達式總是評估爲false。你需要重新思考它們。

P.S.我看着直接的JS版本

+0

** 1 ** - 爲什麼要這麼重要?如果沒有什麼可返回的呢? ** 2 ** - 我已經在事件監聽器之外創建了引用。我沒有引用'this',但是我引用了我所調用的每個事件。你提出的與我已經做過的不同之處是什麼? ** 3 ** - 是的,這是另一個問題,*爲什麼CoffeeScript會這樣做?*將簡單的三元語句變成更復雜的東西,我甚至不能理解? –

+0

我不是在質疑所有事情,只是因爲......我實際上會做你在答案中提出的建議。我問的問題是讓我明白我正在做什麼,下一次有更清晰的畫面。 –

+0

我照你說的做了,它工作了!我想我有**#2 **的答案。當我將'_getTitle'或'_getPurpose'定義爲對'myObject'屬性的引用時,它們仍然是myObject的屬性,但是它們是在'window'的上下文之外執行的事件處理程序)。在myMethod()中定義對this的引用,然後將方法*作爲對象的屬性調用,並在適當的上下文中調用它們。是嗎? –