[英]How to call parent functions from inner nested function?
I have a simple jQuery ready
event that initializes a view by calling the a function in the setupView
object. 我有一个简单的jQuery
ready
事件,该事件通过在setupView
对象中调用a函数来初始化视图。
The question I have is, what is appropriate way to call the function setSomethingImportant
from the init
function as shown below? 我的问题是,从
init
函数调用函数setSomethingImportant
合适方法如下所示?
Since the call is made from a different execution context than the init
function, this.setSomethingImportant()
does not work. 由于调用是从与
init
函数不同的执行上下文中进行的,因此this.setSomethingImportant()
不起作用。 However it works if I use setupView.setSomethingImportant()
. 但是,如果我使用
setupView.setSomethingImportant()
它将起作用。 The problem I have with this is that if the var name ( setupView
) changes, I will have to change the body of the code as well. 我的问题是,如果var名称(
setupView
)更改,我也将不得不更改代码正文。
(function() {
$(document).ready(function() {
setupView.init();
});
var setupView = {
currentState : "CT",
init : function () {
$("#externalProtocol").change( function () {
console.log("Changed =" + $(this).val());
setSomethingImportant();
// Question ? how to call a method in the setupView object
});
},
setSomethingImportant : function () {
this.currentState="TC";
console.log("Something has changed :" + this.currentState );
}
}
}(jQuery);
Store this
into a variable: 保存
this
到一个变量:
var setupView = {
currentState: "CT",
init: function() {
// Keep a reference to 'this'
var self = this;
$("#externalProtocol").change(function() {
console.log("Changed =" + $(this).val());
// Use the old 'this'
self.setSomethingImportant();
});
},
setSomethingImportant: function() {
this.currentState = "TC";
console.log("Something has changed :" + this.currentState);
}
};
See the Working demo . 请参阅工作演示 。
Just declare the function separately and then call like so: 只需分别声明函数,然后像这样调用:
function setSomethingImportant(context) {
context.currentState="TC";
console.log("Something has changed :" + context.currentState );
};
(function() {
$(document).ready(function() {
setupView.init();
});
var setupView = {
currentState : "CT",
init : function () {
$("#externalProtocol").change( function () {
console.log("Changed =" + $(this).val());
setSomethingImportant(this);
// Question ? how to call a method in the setupView object
});
},
setSomethingImportant : function () {
setSomethingImportant(this);
}
}
}(jQuery);
Please note that I changed my original solution. 请注意,我更改了原始解决方案。 I am now passing data to the event handler using even.data .
我现在正在使用Even.data将数据传递给事件处理程序。
(function() {
$(document).ready(function() {
setupView.init();
});
var setupView = {
currentState : "CT",
init : function () {
$("#externalProtocol").change({ _this: this }, function (event) {
console.log("Changed =" + $(this).val());
event.data._this.setSomethingImportant();
});
},
setSomethingImportant : function () {
this.currentState="TC";
console.log("Something has changed :" + this.currentState );
}
}
}(jQuery);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.