2010-06-26 34 views
1

好吧,我想知道是否有可能将对象的引用传递给函数。如果你不明白我尝试说,这可能帮助:从成员方法内访问JavaScript对象引用

//so i declare the variable `Editor` 
var Editor = new (function(e, d){ 
    this.edit = e; 
    this.dyna = d; 
    this.old = ""; //and set these variables inside the object 

    this.update = function() { 
     var ta = $(Editor.edit)[0].value, dy = $(Editor.dyna)[0].contentDocument; 
     //what i want is to be able to refer to the variables (ie. "edit") without using "Editor." 
     if (Editor.old !== ta) { 
      $(dy).text(ta); 
      Editor.old = ta; 
     } 
     window.setTimeout(Editor.update, 150); 
    } 

    return this; 
})("editor","dynamic"); 

所以对于更新功能,我希望能够做一些事情,如:

this.update = function() { 
    var ta = $(edit)[0].value, dy = $(dyna)[0].contentDocument; 
    if (old !== ta) { 
     $(dy).text(ta); 
     old = ta; 
    } 
    window.setTimeout(update, 150); 
} 

,这让我的变量(编辑,dyna,旧)从Editor对象。 谢谢。

回答

1

this您的函数内部指的是您创建的匿名基础函数的对象。使用this.propertyName来访问其属性。

var ta = $(this.edit)[0].value, dy = $(this.dyna)[0].contentDocument; 
+0

谢谢。这对我来说不是很聪明,但我试图解决一个更大的问题并且感到困惑。回到正轨... – tcooc 2010-06-26 03:10:56

2

为什么不只是使用this前缀。那么this.edit[0].value

也许我错过了一些东西,因为它在这里很晚...

相关问题