【发布时间】:2011-07-07 17:48:42
【问题描述】:
如何传入 Class 实例的“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和alert(this)是什么,看看发生了什么。
标签: javascript jquery oop this