Код: Выделить всё
class A{
constructor(){
this.n = 0;
}
z(){
this.n++;
}
}
function wrap(p){
p();
}
a = new A();
a.z();
console.log(a.n);
wrap(a.z); // "this" is "lost" and undefined
console.log(a.n); // not printed
Код: Выделить всё
class A:
def __init__(self):
self.n = 0
def z(self):
self.n += 1
def wrap(p):
p()
a = A()
a.z()
print(a.n)
wrap(a.z) # "self" preserved
print(a.n)
Подробнее здесь: https://stackoverflow.com/questions/798 ... ting-it-wh
Мобильная версия