假设我们有一个名为aObject的Javascript对象,并且test()函数被用作JQuery的回调函数。
如果这似乎与问题有关,请通知我。
var aObject = {
aVariable : 'whatever value',
test : function() {
// Trying to access property. But doesn't work as expected since I am getting the DOM element, not the aObject reference
var temp = this.aVariable;
}
}
var anInstanceOfAObject = $.extend({}, aObject);
anInstanceOfAObject.someFunction = function () {
// I have to put "this" in a variable since "this" in the context below refers to the DOM element, not the instance of the object
var placeHolder = this;
$('some random div.element').theJavascriptFunction({
"theJavascriptCallbackFunction": placeHolder.test,
});
}
在test()函数内部,通常情况下“this”的上下文是DOM元素。我的问题是如何引用aObject,因为我们不能使用“this”来引用它。
编辑:我不确定上面的语法是否是实例化对象的正确/首选方式。我看到一些示例使用这种语法。
var aObject = function() {....
如果这似乎与问题有关,请通知我。