作爲一個正在嘗試採用更加面向對象的方法來編寫我的JavaScript編程的人,我碰到了一個絆腳石,我敢肯定它可能是非常基本的東西,但是,採取以下對象實現(假設jQuery對象是提供給這個代碼):JavaScript作用域問題
function Foo()
{
this.someProperty = 5;
}
Foo.prototype.myFunc = function()
{
//do stuff...
};
Foo.prototype.bar = function()
{
//here 'this' refers to the object Foo
console.log(this.someProperty);
$('.some_elements').each(function()
{
//but here, 'this' refers to the current DOM element of the list of elements
//selected by the jQuery selector that jquery's each() function is iterating through
console.log(this);
//so, how can i access the Foo object's properties from here so i can do
//something like this?
this.myFunc();
});
};
我知道它會是這樣簡單,謝謝:-) – 2010-09-08 17:44:22