如何在同一个对象中调用另一个函数?

时间:2022-08-07 23:56:24
let role = {
    test: (variable) => {
        // How do I call toLog(variable) from here?
    },
    toLog: (variable) => {
        console.log(variable);
    }
};

I'd like to call the toLog() function within the test() function but I'm unaware how to.

我想在test()函数中调用toLog()函数,但是我不知道如何调用。

1 个解决方案

#1


11  

Standard JS functions use dynamic binding, this is dependent on who's calling the method on runtime, so if we call it using role.test(), it will bind this to role.

标准的JS函数使用动态绑定,这取决于谁在运行时调用这个方法,所以如果我们使用roley .test()调用它,它将把它绑定到角色。

Arrow functions bind this to the current context. For example, if the code was writtern in the browser's console, this is bound to the window object. This is called static lexical binding, which means binding this to the closure it was defined in.

箭头函数将其绑定到当前上下文。例如,如果代码是在浏览器的控制台中写入的,那么这将绑定到窗口对象。这被称为静态词法绑定,这意味着将它绑定到它在其中定义的闭包。

If you won't use arrow functions, this will point to the object itself when called by role:

如果不使用箭头函数,则在角色调用时指向对象本身:

const role = {
    test(variable){
        this.toLog(variable);
    },
    toLog(variable) {
        console.log(variable);
    }
};

role.test(5);

In this case, we don't want to bind this to the outer context, so we'll skip the static binding in favor of the dynamic one.

在这种情况下,我们不希望将它绑定到外部上下文,因此我们将跳过静态绑定,而使用动态绑定。

However, if we'll use this method as a callback, dynamic binding will change this according to who's running the method. To prevent that, we'll have to use bind to create an explicit static binding to role.

但是,如果我们将此方法用作回调,动态绑定将根据运行该方法的人更改此方法。为了避免这种情况,我们必须使用bind来创建对角色的显式静态绑定。

const role = {
  test(variable) {
      this.toLog(variable);
    },
    toLog(variable) {
      console.log(variable);
    }
};

let test = role.test;

try {
  test(20); // will throw an error - this.toLog is not a function - because this points to window
} catch (e) {
  console.log(e);
}

test = role.test.bind(role);

test(25); // will work because it's staticly binded to role

#1


11  

Standard JS functions use dynamic binding, this is dependent on who's calling the method on runtime, so if we call it using role.test(), it will bind this to role.

标准的JS函数使用动态绑定,这取决于谁在运行时调用这个方法,所以如果我们使用roley .test()调用它,它将把它绑定到角色。

Arrow functions bind this to the current context. For example, if the code was writtern in the browser's console, this is bound to the window object. This is called static lexical binding, which means binding this to the closure it was defined in.

箭头函数将其绑定到当前上下文。例如,如果代码是在浏览器的控制台中写入的,那么这将绑定到窗口对象。这被称为静态词法绑定,这意味着将它绑定到它在其中定义的闭包。

If you won't use arrow functions, this will point to the object itself when called by role:

如果不使用箭头函数,则在角色调用时指向对象本身:

const role = {
    test(variable){
        this.toLog(variable);
    },
    toLog(variable) {
        console.log(variable);
    }
};

role.test(5);

In this case, we don't want to bind this to the outer context, so we'll skip the static binding in favor of the dynamic one.

在这种情况下,我们不希望将它绑定到外部上下文,因此我们将跳过静态绑定,而使用动态绑定。

However, if we'll use this method as a callback, dynamic binding will change this according to who's running the method. To prevent that, we'll have to use bind to create an explicit static binding to role.

但是,如果我们将此方法用作回调,动态绑定将根据运行该方法的人更改此方法。为了避免这种情况,我们必须使用bind来创建对角色的显式静态绑定。

const role = {
  test(variable) {
      this.toLog(variable);
    },
    toLog(variable) {
      console.log(variable);
    }
};

let test = role.test;

try {
  test(20); // will throw an error - this.toLog is not a function - because this points to window
} catch (e) {
  console.log(e);
}

test = role.test.bind(role);

test(25); // will work because it's staticly binded to role