首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在对象文本中的回调中使用“this”

在对象文本中的回调中使用“this”
EN

Stack Overflow用户
提问于 2012-12-21 20:45:44
回答 3查看 355关注 0票数 0

可能重复: Javascript prototype ‘this’ issue

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

有一个注释表示行为不被理解。this_hold.Name不能像我想的那样在那里访问。

代码语言:javascript
复制
/*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);
        }
    }
});
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-12-21 21:02:16

this是生成事件的DOM对象。它不是您的javascript对象。

当您将this.enter作为事件处理程序的方法传递时,enter方法不会绑定到您的对象。如果您希望这样的情况发生,您必须更改代码,以便通过这样的操作导致这种情况发生:

代码语言:javascript
复制
// save local copy of my object so I can refer to it in
// the anonymous function
var obj = this;
this.E.text.addEventListener("keypress", function(event) {obj.enter(event)}, false);

重要的是要记住,this是由方法/函数的调用方设置的。在这种情况下,事件处理程序的调用方是浏览器中的事件子系统。它不知道您的对象是什么,它的设计行为是将this设置为导致事件的DOM对象。因此,如果您想要调用您的obj.enter方法,就不能仅仅将enter作为事件处理程序传递。相反,您将创建一个单独的函数,该函数被调用为事件处理程序,然后使用对象作为基础调用obj.enter(),以便正确设置this

另一种解决方案是使用.bind(),它还创建一个存根函数,它将正确的this绑定到函数调用,但我自己不使用.bind(),因为它并不适用于所有旧浏览器。

票数 1
EN

Stack Overflow用户

发布于 2012-12-21 20:48:09

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

代码语言:javascript
复制
console.log(this);
票数 2
EN

Stack Overflow用户

发布于 2012-12-21 20:48:16

尝试更改事件的绑定方式。

代码语言:javascript
复制
this.E.text.addEventListener("keypress", this.enter, false);

代码语言:javascript
复制
var that = this;
this.E.text.addEventListener("keypress", function(event) {
  that.enter(event);
}, false);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13996985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档