该代码非常具有解释性。我正在做的事情是错误的。如何在 A
对象方法内声明的对象的 onclick
事件中访问 A 对象的 a 属性?
function A(){
this.a = 0;
};
A.prototype.myfun= function(){
var b = document.getElementsByClassName("myclassName");
b[0].onclick = function(e){
//How can I get the a property of the A object in here?
this.a = 1;
}
};
我可以以某种方式将其作为这样的参数传递吗?
b[0].onclick = function(e, this){
最佳答案
由于函数中的 this
引用了函数本身,因此您可以做两件事。传递引用,或者创建一个不会覆盖的代表 this
function A(){
this.a = 0;
};
A.prototype.myfun= function(){
var self = this;
var b = document.getElementsByClassName("myclassName");
b[0].onclick = function(e){
self.a = 1;
}
};
关于javascript - JS中如何访问父类(super class)的super?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17252250/