close

學習case 1

使用express時,這樣使用

app.js
app.get('/musicbox', musicbox.getBoxInfo);
musicbox.js
musicbox = function() {

}
musicbox.prototype.getBoxInfo = function(req, res) {
this.xxx
}

發現 在musicbox中的getBoxInfo的this 不是指向musicbox本身的,而是指向外部變數所以改寫成以下,改成閉包的方式

musicbox = function() {
    var self = this;
    this.getBoxInfo = function(req, res) {
        self.xxx
    }
}

學習case 2

event trigger的this是聽那個event的obj. 以底下例子來說,this就是player

player.on('end', function() {
this.xxx
}

Reference link: https://software.intel.com/zh-cn/blogs/2013/10/09/javascript-this

arrow
arrow
    文章標籤
    javascript
    全站熱搜

    Perry Wu 發表在 痞客邦 留言(0) 人氣()