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

//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,old) Editor 目的。谢谢。

有帮助吗?

解决方案

this 在您的功能中,指您创建的匿名基本函数的对象。

采用 this.propertyName 访问其属性。

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

其他提示

你为什么不只是使用 this 字首。所以 this.edit[0].value?

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top