我經常看到Javascript代碼,其中this
被分配給一個變量用於引用對象的當前實例。'this'在被使用之前被分配給一個var?
var me = this;
me.someproperty_or_method
他們爲什麼要這樣編碼? ?這裏是一個更完整的代碼片段,
var Preload = function(game){};
Preload.prototype = {
init: function() {
var me = this;
var style = {
font: "32px Arial",
fill: "ffffff",
align: "center"
};
this.text = this.add.text(me.game.world.centerX, me.game.world.centerY, "Loading: 0%", style);
this.text.anchor.x = 0.5;
}
preload: function(){
this.game.load.text('dictionary', 'assets/dictionary.txt');
},
create: function(){
this.game.state.start("Main");
}
}
我很確定這不是重複的,在其他帖子上給出的答案是不確定的。
'me'可能被用在回調函數中,在這種情況下'this'將會有所不同 – Fabricator
這段代碼沒有任何意義。他們應該在這裏使用'this.someproperty_or_method'。如果他們需要一個'var me'來引用這個'this'的值,那麼這是一個單獨的事情。 – Bergi