[英]Access property in class from prototype override
Not sure if my question is worded properly.不确定我的问题是否措辞正确。 But basically I have a class, but I want to write a new method for it.
但基本上我有一个类,但我想为它编写一个新方法。 Say my class looks like this:
说我的课是这样的:
class MyClass {
constructor () {
this.someProperty = []
this.someMethod = this.someMethod.bind(this)
}
function someMethod () {
// do something
}
}
Now, because I don't have direct access to this class, I am going to create a new method using prototype
现在,因为我不能直接访问这个类,我将使用
prototype
创建一个新方法
MyClass.prototype.myNewMethod = function (params) {
// do something else
// how to access someProperty? And to the bind to MyClass?
}
But now say I want to access someProperty
and also want to do the bind
on this new method.但是现在说我想访问
someProperty
并且还想对这个新方法进行bind
。 How can I do that?我怎样才能做到这一点?
As a matter of fact is my method creation even correct to begin with?事实上,我的方法创建一开始就正确吗? Anyway, what I want is for it to have the same access to the
this
inside the class.无论如何,我想要的是让它在类中拥有对
this
的相同访问权限。 How can I do this?我怎样才能做到这一点?
I didn't understand what you want to do but so you can access it:我不明白你想做什么,但你可以访问它:
class MyClass {
constructor () {
this.someProperty = [];
}
someMethod = function() {
console.log(this.someProperty);
}
}
var cc = new MyClass();
cc.someMethod();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.