内部メンバーメソッドからJavaScriptオブジェクト参照にアクセスします

StackOverflow https://stackoverflow.com/questions/3122537

質問

わかりました、私はオブジェクトの参照を関数に転送できるかどうか疑問に思っています。あなたが私が何を言おうとしているのか理解していないなら、これは役立つかもしれません:

//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 物体。ありがとう。

役に立ちましたか?

解決

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