學習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
文章標籤
全站熱搜
