如何传入类实例的“this”上下文的变量代理?例如, this.saySomething() 没有做我想做的事情。
您对 OOJS 代码组织还有其他建议吗?
// Truveo Video Player Class
function Player(){
// Player Defaults
this.opts = {
volume: 0 // Initial volume settings
};
}
// Initialize player setup / methods
Player.prototype.init = function(configs){
// Overwrite defaults with custom configs
this.opts = $.extend(this.opts, configs);
$(document).ready(function(){
this.saySomething();
});
$(window).load(function(){
// do something else
});
}
Player.prototype.saySomething = function(){
alert(this.opts.volume);
}
// Create instance for channel surf
var configs = {volume:10};
var cSurf = new Player();
cSurf.init(configs);
最佳答案
在进入函数之前保存this
的副本:
var me = this;
$(document).ready(function(){
me.saySomething();
});
关于javascript - 在原型(prototype)中的 $.ready 中使用 "this",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5175787/