获取 ES6 class 的 "this" 以调用方法并获取数据成员
Getting "this" of an ES6 class to call methods and get data members
我想从 class 内部调用其他 class 方法,但是当某些方法调用某个方法时,我无法访问 this
,因为现在 this
是调用方法,不能使用其他 class 方法或获取数据成员。
例如:
class someclass{
_this = this;
foo(){
this.bar();
}
bar(){
this.baz(); // this is foo not someclass
_this.baz(); // _this is not defined
}
baz(){
}
}
那么我如何才能始终访问实际的 class 以便能够调用其方法并从其方法中使用其数据成员?
编辑:在我的实际代码中,我有另一个对象调用 foo
事件,所以 this
是输入 foo
而不是 someclass
时的那个对象.
Class Javascript 中的方法默认不绑定。意义
this
价值取决于它们的调用方式,而不是它们的调用方式
已定义。
在ES6中绑定(保持this
):
class SomeClass {
constructor() {
// This binding maintains the value of this
// inside these methods during future calls.
this.foo = this.foo.bind(this)
this.bar = this.bar.bind(this)
this.baz = this.baz.bind(this)
}
foo() {
this.bar();
console.log('from foo')
}
bar() {
this.baz(); // This is foo not SomeClass
console.log('from bar')
}
baz() {
}
}
// If you are using Babel you can use arrow function/methods
class SomeClass {
foo = () => {
this.bar();
console.log('from foo')
}
bar = () => {
this.baz(); // This is foo not SomeClass
console.log('from bar')
}
baz() {
}
}
const s = new SomeClass()
s.foo()
控制台输出:
"from bar"
"from foo"
class someClass2 {
bar() {
console.log('bar from someClass2')
}
}
class someClass {
constructor() {
//instantiate an object of someClass2 to access all itsmethod
this.obj2 = new someClass2()
}
foo () {
console.log('foo from someClass')
}
barFromObj2(){
return this.obj2.bar()
}
}
var obj = new someClass();
obj.foo(); // return foo from someClass
// access the bar method from the someClass2
obj.barFromObj2(); // return bar from someClass2
我想从 class 内部调用其他 class 方法,但是当某些方法调用某个方法时,我无法访问 this
,因为现在 this
是调用方法,不能使用其他 class 方法或获取数据成员。
例如:
class someclass{
_this = this;
foo(){
this.bar();
}
bar(){
this.baz(); // this is foo not someclass
_this.baz(); // _this is not defined
}
baz(){
}
}
那么我如何才能始终访问实际的 class 以便能够调用其方法并从其方法中使用其数据成员?
编辑:在我的实际代码中,我有另一个对象调用 foo
事件,所以 this
是输入 foo
而不是 someclass
时的那个对象.
Class Javascript 中的方法默认不绑定。意义
this
价值取决于它们的调用方式,而不是它们的调用方式
已定义。
在ES6中绑定(保持this
):
class SomeClass {
constructor() {
// This binding maintains the value of this
// inside these methods during future calls.
this.foo = this.foo.bind(this)
this.bar = this.bar.bind(this)
this.baz = this.baz.bind(this)
}
foo() {
this.bar();
console.log('from foo')
}
bar() {
this.baz(); // This is foo not SomeClass
console.log('from bar')
}
baz() {
}
}
// If you are using Babel you can use arrow function/methods
class SomeClass {
foo = () => {
this.bar();
console.log('from foo')
}
bar = () => {
this.baz(); // This is foo not SomeClass
console.log('from bar')
}
baz() {
}
}
const s = new SomeClass()
s.foo()
控制台输出:
"from bar"
"from foo"
class someClass2 {
bar() {
console.log('bar from someClass2')
}
}
class someClass {
constructor() {
//instantiate an object of someClass2 to access all itsmethod
this.obj2 = new someClass2()
}
foo () {
console.log('foo from someClass')
}
barFromObj2(){
return this.obj2.bar()
}
}
var obj = new someClass();
obj.foo(); // return foo from someClass
// access the bar method from the someClass2
obj.barFromObj2(); // return bar from someClass2