为什么在对象方法中未定义?

时间:2017-10-24 00:19:14

标签: javascript function this undefined

我有 loginView 对象,它有2个函数 - initLogin goAuth 。在这些方法中,我使用关键字 this 。使用 initLogin 时,我为 this.loginField 设置了值,对于其他一些变量,一切正常。但是当我尝试调用goAuth时,它说 this.loginField 是"未定义的参数"。顺便说一句,我在 goAuth 之前调用 initLogin 。此外,如果我使用 loginView.pane - 一切正常。

那么,为什么

代码:

var loginView = {
pane: null,

hideButton: null,
exitButton: null,

loginField: null,
passwordField: null,
savePasswordBox: null,
enterButton: null,

registerButton: null,
forgotButton: null,

mask: null,

movePoint: null,

initLogin: function(){
    this.pane = loadFXML("dialog/login.fxml");
    LogHelper.info(this.pane==loginView.pane);
    this.pane.setOnMousePressed(function(event){ loginView.movePoint = new javafx.geometry.Point2D(event.getSceneX(), event.getSceneY())});
    this.pane.setOnMouseDragged(function(event) {
        if(loginView.movePoint === null) {
            return;
        }

        // Обновляем позицию панели
        stage.setX(event.getScreenX() - loginView.movePoint.getX());
        stage.setY(event.getScreenY() - loginView.movePoint.getY());
    });

    // Lookup hide & exit button
    this.hideButton = this.pane.lookup("#hidebtn").setOnAction(function(event){ stage.setIconified(true)});
    this.exitButton = this.pane.lookup("#exitbtn").setOnAction(function(event){ javafx.application.Platform.exit()});

    // Lookup login field
    this.loginField = this.pane.lookup("#logintf");
    this.loginField.setOnAction(this.goAuth);
    if (settings.login !== null) {
        this.loginField.setText(settings.login);
    }

    // Lookup password field
    this.passwordField = this.pane.lookup("#passwordtf");
    this.passwordField.setOnAction(this.goAuth);
    if (settings.rsaPassword !== null) {
        this.passwordField.setPromptText("*** Сохранённый ***");
    }

    // Lookup save password box
    this.savePasswordBox = this.pane.lookup("#rememberchb");
    this.savePasswordBox.setSelected(settings.login === null || settings.rsaPassword !== null);

    // Lookup action buttons
    this.enterButton = this.pane.lookup("#enterbtn").setOnAction(this.goAuth);
    this.registerButton = this.pane.lookup("#registerbtn").setOnAction(function(){openURL(config.registerURL)});
    this.forgotButton = this.pane.lookup("#forgotbtn").setOnAction(function(){openURL(config.forgotURL)});

    // Init overlays
    //processing.initOverlay();

    // Verify launcher & make request
    //verifyLauncher();
},

goAuth: function()
    // doesn't work, "loginField is undefined for this" 
    var login = this.loginField.getText();
    // does work
    //var login = loginView.loginField.getText();
    if (login.isEmpty()) {
        //TODO: some sort of exception
        return;
    }

    // Get password if online-mode
    var rsaPassword = null;
    if (!this.passwordField.isDisable()) {
        var password = this.passwordField.getText();
        if (!password.isEmpty()) {
            rsaPassword = settings.setPassword(password);
        } else if (settings.rsaPassword !== null) {
            rsaPassword = settings.rsaPassword;
        } else {
            return;
        }

        // Remember or reset password
        settings.rsaPassword = this.savePasswordBox.isSelected() ? rsaPassword : null;
    }

    // Show auth overlay
    settings.login = login;
    doAuth(login, rsaPassword);
}
};

0 个答案:

没有答案