比方说,我们有一个名为aObject JavaScript对象和测试()函数用作回调函数的JQuery如何在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,
});
}
内部的测试()函数中,“本”是的DOM正常范围内元件。我的问题是如何引用aObject,因为我们不能使用“this”来引用它。
编辑:我不知道如果上面的语法是正确的/首选的方式来实例化一个对象。我看到一些使用此语法的示例
var aObject = function() {....
请告知我,看起来这是否与问题有关。
这个你的意思aObject吧? – Baz1nga
是的。我想获得aObject,而不是DOM元素(如果有两种方法可以更好)。 – arvinsim