比方說,我們有一個名爲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