简体   繁体   English

从另一个上下文中调用方法时,“ this”是未定义的

[英]`this` is undefined when calling method from another context

This is my first time creating OOP for JS. 这是我第一次为JS创建OOP。 I followed some tutorials but I can't wrap my head around this issue. 我遵循了一些教程,但是无法解决这个问题。 I know the problem, but i dont know the solution 我知道问题所在,但我不知道解决方案

function NewApp(name){
    this.name = name;
    this.currentPage = 1;
    this.customObjectWithMethods = //init with options and so on
}

NewApp.prototype.logic = function(){
// Note 1.  
    var app = this
    //Note 3.
    this.customObjectWithMethods.method{
        if(app.currentpage < 3)
            // Note 2.  
            app.navigate(app.logic)
    }
}

NewApp.prototype.navigate = function(sender){
    var app = this;
    this.customObjectWithMethods.method{
        app.currentpage++;
        this.method(function() {
            return app.currentPage === 2;
        }, sender(), this.terminate);
    } 
}
  • Note 1: I need to create a reference because after that, this doesn't work anymore to refer to the current object. 注意1:我需要创建一个引用,因为在那之后, this引用不再适用于引用当前对象。
  • Note 2: After the check I want to do some logic in another method and repeat the current function, but when the function runs again it breaks on the method ( this.customObjectWithMethods ) because this doesn't exists. 注意2:检查后,我想在其他方法中做一些逻辑this.customObjectWithMethods并重复当前函数,但是当函数再次运行时,它在方法( this.customObjectWithMethods )上中断,因为this方法不存在。
  • Note 3: This is where it breaks because "this" works the first time not the second time. 注意3:这是中断的地方,因为“ this”是第一次而不是第二次起作用。

It gets very complicated like this with the this -keyword, which makes me think that my design may be flawed. this -keyword会使事情变得非常复杂,这让我觉得我的设计可能有缺陷。

Is there any solution for this problem, or should I refactor it ? 有没有解决这个问题的方法,还是我应该重构它?

Some syntax errors & style issues - here is a short correction 一些语法错误和样式问题-这是一个简短的更正

var myFunction = function(){
    //code here
};

var mySecondFunction = function(){
    //code here
};

function NewApp(name){
this.name = name;
this.currentPage = 1;
this.customObjectWithMethods = function(){}; //empty function so calling doesnt resolve in error

}

NewApp.prototype.logic = function(){

   this.customObjectWithMethods.method = mySecondFunction.bind(this);
}

NewApp.prototype.navigate = function(sender){

    this.customObjectWithMethods.method = myFunction.bind(this);

}

I have moved the 2 functions outside of the constructor Function so they dont get recreated every time you call the constructor functions. 我已经将2个函数移到了构造函数的外面,所以每次调用构造函数时都不会重新创建它们。

with _.bind(this) the "this"-reference gets passed into the scope of your functions (i think this is more pretty than creating another var). 使用_.bind(this),“ this”引用被传递到您的函数范围内(我认为这比创建另一个var更漂亮)。

with

var reff = new NewApp('namename');

you can get started calling your functions now: 您可以立即开始调用函数:

ref.logic(); 

maybe this approach works for you? 也许这种方法对您有用?

Surely it will become complicated, this keyword doesn't always refer to the main object but to the scope where it is used, take a look at Scope and this in JavaScript for further information. 当然它将变得复杂, this关键字并不总是指向主要对象,而是指向其使用范围,请查看Scope以及JavaScript中的更多信息。

This is your way to go, make a variable that contains your constructor and add these two methods to this variable, after that you can call your functions: 这是您要采取的方法,创建一个包含构造函数的变量,然后将这两个方法添加到此变量中,之后您可以调用函数:

var newApp = function newApp(name){
    this.name = name;
    this.currentPage = 1;

    //Make a reference to your object here
    var THIS = this;

    this.logic = function(){ 
        var sender = this;

        THIS.customObjectWithMethods.method = function(){
            if(THIS.currentpage < 3) 
                THIS.navigate(sender);
        }
    }

    this.navigate = function(sender){
        this.customObjectWithMethods.method = function(){
            THIS.currentpage++;
            this.method(function() {
                return THIS.currentPage === 2;
            }, sender(), this.terminate);
        } 
    }    

}

And this is how to use your constructor and its methods: 这就是如何使用构造函数及其方法:

var app = newApp("Test");

//Call the first method
app.customObjectWithMethods();

//Thenn call the second one
app.logic();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从另一个调用原型方法-出现“未定义”错误 - calling a prototype method from another - getting 'undefined' error 从另一个文件导入时方法未定义 - Method is undefined when importing from another file 从另一个js源文件调用函数时发生未定义的错误 - undefined error when calling function from another js sourcefile 从另一个 class 调用 function 时属性未定义 - Property undefined when calling a function from another class 从另一个 function 调用 function 时获取未定义的变量 - Getting undefined variable when calling function from another function 从另一个 function javascript 调用异步 function 时变得未定义 - Getting undefined when calling an async function from another function javascript 从$ ajax.success调用时方法未定义 - Method undefined when calling it from $ajax.success 从另一个方法调用方法时的jQuery插件(此)混乱 - jQuery plugin (this) confusion when calling method from another method 从一个类调用方法时从另一个类调用方法 - Call method from one class when calling methods from another 调用ES6方法时绑定上下文。如何从称为回调的方法中访问对象? - Binding context when calling ES6 method. How to access object from within method called as callback?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM