2010-06-26 35 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

也許我錯過了一些東西,因爲它在這裏很晚...

相關問題