javascript - 在对象字面量的回调中使用 'this'

标签 javascript

Possible Duplicate:
Javascript prototype ‘this’ issue

我有一个事件监听器,它当然会调用事件的方法。此方法尝试保存对持有对象 this 的引用,以便它可以访问该对象的其他属性,但未成功。

有一个注释表示该行为不被理解。 this_hold.Name 在那里无法访问,正如我想象的那样。

/*MUserExist
**
**
**
*/
$A.module({
    Name: 'MUserExist',
    S: {
        ClientStorage: SClientStorage,
        ComMessage: SComMessage,
        ComText: SComText,
        DynSma: SDynSma,
        DynTwe: SDynTwe,
        DynArc: SDynArc,
        AniMorphLabel: SAniMorphLabel,
        AniFlipPage: SAniFlipPage
    },
    E: {
        but: $A('#ue_but')[0],
        text: $A('#ue_go')[0],
        form: $A('#ue_fo')[0],
        check: $A('#ue_check')[0]
    },
    J: {
        box: $('#ue_box')
    },
    init: function () {
        var pipe = {},
            this_hold = this;
        this.J.box.draggable();
        this.E.but.addEventListener("click", function () {
            pipe = $A.definePipe(this_hold.Name);
            $A.machine(pipe);
        }, false);
        this.E.text.addEventListener("keypress", this.enter, false);
        this.S.AniMorphLabel.run(["ue_email",
                                  "ue_email_lab",
                                  "ue_go",
                                  "ue_pass_lab"
                                 ]);
    },
    enter: function (event) {
        var pipe = {},
            this_hold = this;
        if (event.keyCode === 13) {
            pipe = $A.definePipe(this_hold.Name); // fails here what does 'this' point to?
            $A.machine(pipe);
            event.preventDefault();
        }
    },
    pre: function (pipe) {
        var form_elements = this.E.form.elements,
            text_object = new this.S.ComText(form_elements);
        pipe.enter = this.enter;
        if ($A.Un.get('load') === '1') {
            if (!text_object.checkFull()) {
                pipe.type = 'empty';
                return this.S.ComMessage.message(pipe);
            }
            if (!text_object.checkPattern('email')) {
                pipe.type = 'email';
                return this.S.ComMessage.message(pipe);
            }
            if (!text_object.checkPattern('pass')) {
                pipe.type = 'pass';
                return this.S.ComMessage.message(pipe);
            }
        }
        pipe.page = text_object.getArray();
        pipe.proceed = true;
        pipe.page.remember = this.E.check.checked;
        return pipe;
    },
    post : function (pipe) {
        if (pipe.proceed === true) {
            this.S.ComMessage.resetView('ue_email');
            this.S.ComMessage.resetView('ue_go');
            this.S.ClientStorage.setAll(pipe.server.smalls);
            this.S.DynSma.run(pipe.server.smalls);
            this.S.DynArc.run(pipe.server.arcmarks);
            this.S.DynTwe.run(pipe.server.tweets);
            this.S.AniFlipPage.run('ma');
        } else {
            return this.S.ComMessage.message(pipe);
        }
    }
});

最佳答案

this 可能指向触发事件的 DOM 节点。您是否尝试过将 this 写入控制台来检查它?

console.log(this);

关于javascript - 在对象字面量的回调中使用 'this',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13996985/

相关文章:

javascript - SVG路径绘制和删除动画

javascript - 自定义 "mouseenter"函数未触发

javascript - 数组不响应 forEach

javascript - 根据IP地址显示图片

javascript - 使用 encode_json 后重音字符被双重编码

javascript - 将 Angular2 TypeScript 文件和 JavaScript 文件分离到不同的文件夹中,可能是“dist”

javascript - 在 Three.js 中合并纹理网格

javascript - 简单的 Ajax jQuery get 方法未完成

javascript - 我如何制作这样的番茄钟?

Javascript onscroll 函数不起作用