我写了以下代码。此处getGreeting
已与Salutation
绑定。当getGreeting
使用myName
与bind()
绑定时,仍会使用Salutation
类的上下文调用
let myName = {
name: 'John'
}
class Salutation {
constructor() {
this.name = 'Mike';
this.getGreeting = this.getGreeting.bind(this)
}
getGreeting() {
return `Hi. My name is ${this.name}`
}
}
const salutation = new Salutation();
console.log(salutation);
boundFunction = salutation.getGreeting.bind(this);
boundFunction();// My name is Mike
boundFunction = salutation.getGreeting.bind(myName);
boundFunction();// My name is Mike
答案 0 :(得分:6)
你不能。
bind
创建一个新函数(B),它使用特定this
调用原始函数(A)。
尝试重新绑定它会创建另一个新函数(C),它使用特定的this
调用前一个函数(B)。但是,B并不关心this
的值是什么,它仍然使用最初被告知使用的值来调用A.
答案 1 :(得分:0)
我唯一能做的就是在班级内部玩一个上下文:
- name: loop of uri module over the hosts group
uri:
url: http://{{ item }}/
return_content: yes
register: webpage
with_items:
- "{{ groups['webserver'] }}"