作为一个正在尝试采用更加面向对象的方法来编写我的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