简体   繁体   English

两个类一个引用函数到另一个不同的 this 值

[英]Two classes one with reference to function to another different this value

I have problem and I don't know how to solve it:我有问题,我不知道如何解决:

        this.bWords.push(word);
                    ^

TypeError: Cannot read property 'push' of undefined类型错误:无法读取未定义的属性“推送”

here is my code:这是我的代码:

function multiWords(words) {
    class AWords {
        constructor(words = []) {
            this.words = words;
            this.addWordFn = () => {};
        }

        setAddWordFn(fn) {
            this.addWordFn = fn;
        }

        passWords() {
            this.words.forEach(word => this.addWordFn(word));
        }
    }

    class BWords {
        constructor() {
            this.bWords = [];
        }

        addWord(word) {
            this.bWords.push(word);
        }
    }

    let x = new AWords(words);
    let y = new BWords();

    x.setAddWordFn(y.addWord);
    x.passWords();

    return y.bWords;
}


console.log(multiWords(["one", "two", "three"]));

Do you have any ideas why there is different this value?你有什么想法为什么会有不同的this值? Many thanks Pati非常感谢帕蒂

It appears that the problem occurs here:看来问题出现在这里:

this.words.forEach(word => this.addWordFn(word));

because the function you've set for addWordFn here:因为您在此处为addWordFn设置的功能:

x.setAddWordFn(y.addWord);

Needs a different value of this than you are calling it with.需求的不同值this比你与调用它。 You can fix it by binding the right value of this to your callback:您可以通过将 this 的正确值绑定到您的回调来修复它:

x.setAddWordFn(y.addWord.bind(y));

Remember that for regular functions, the value of this inside the function is determined by how the function is called.请记住,对于常规函数,函数内部的this值取决于函数的调用方式。 When you call a function with obj.method() , the this value inside of method() will be set to obj .当你调用一个函数obj.method()this值内method()将被设置为obj So, you're calling addWord with the wrong this value because you've made it a method of some other object (that does not also have the data it needs) and are calling it off that object.因此,您使用错误的this值调用addWord是因为您已将其设置为某个其他对象的方法(该对象也没有所需的数据),并且正在从该对象调用它。

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

相关问题 在不同的时间向一个 function 添加两个不同的类 javascript - Adding two different classes to one function at a different time javascript 类和函数构造函数之间有何不同 - How are Classes and Function Constructors different from one another 从一个函数为不同的变量取两倍的不同值 - take two times different value for different variable from one function 在两个不同的类上运行Javascript Jquery函数 - Run Javascript Jquery function on two different classes 编码具有相同 function 但类不同的两个按钮 - Coding Two buttons with the same function but different classes 一个属性值是否可以作为参数传递给另一个属性,其值是通过 function 计算的? (在带类的 Javascript ES6 中) - Can one property value be passed as a argument to another property whose value is computed via a function ? (in Javascript ES6 with classes) Select Element 与 Cypress 中的两个不同类之一 - Select one of two different classes within Element with Cypress 使用另一个数组在 for 循环中引用两个不同数组的语法? - Syntax to reference two different arrays within a for loop using another array? 如何将数组从一个函数传递到另一个函数作为Cloud Function中的引用 - How to pass an array from one function to another as reference in Cloud Function 如何获得一个功能的价值给另一个功能 - how to get value of one function to another function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM