1

好的,我想知道是否可以将对象的引用转移到函数中。如果您不明白我想说什么,这可能会有所帮助:

//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);
}

它给了我Editor对象中的变量(编辑、动态、旧)。谢谢。

4

2 回答 2

2

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

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

于 2010-06-26T03:04:01.577 回答
1

this你的函数内部是指你创建的匿名基函数的对象。

用于this.propertyName访问其属性。

var ta = $(this.edit)[0].value, dy = $(this.dyna)[0].contentDocument;
于 2010-06-26T03:03:21.930 回答