![](/img/trans.png)
[英]Accessing class member variables inside an event handler in Javascript
[英]Javascript and jQuery: accessing class member variables from event callback
我正在編寫一個Javascript SDK與Web服務進行交互。 我正在使用jQuery進行AJAX調用。
當AJAX調用失敗時,我已經為ajaxError注冊了事件處理程序,該事件處理程序在.js文件的頂部被調用。 我的問題(我不理解為什么)是,當調用它時,我無法訪問Akamanda.Client的類成員變量。
我嘗試為Akamanda.Client添加另一個方法作為.prototype.logError,該方法由jQuery Ajax處理程序調用,但即使這樣(.logging)的測試也失敗了。
如何從jQuery回調訪問類成員變量? 我在這里看不懂什么? 從ajaxError回調中未定義Akamanda.Client.logging。
我的SDK代碼:
$(document).ajaxError(function(event, jqxhr, settings, exception) {
// more robust error handling for different conditions
if (Akamanda.Client.logging) {
console.log('FAILED: ' + settings.type + ' ' + settings.url + ' => ' + exception);
}
});
Akamanda.Client = function(options) {
this.URL = options.URL || 'http://m-test.akamanda.com';
this.baseURL = this.URL + '/api/' + Akamanda.API_VERSION;
this.feedsURI = '/websyndication/feed/';
// who is the client? (iphone/android/web)
this.clientName = options.clientName;
// For development: Logging and buildcurl IS ON, for production: OFF
//this.logging = options.logging || true;
this.logging = true;
// called when a user is not authorised (Disabled)
// this.logoutCallback = options.logoutCallback || null;
}
Akamanda.Client.prototype.getFeeds = function(callback){
var feeds = [];
$.getJSON(this.baseURL + this.feedsURI, function(data) {
$.each(data, function(index, feed) {
feeds[index] = {
name: feed.name,
title: feed.title,
link: feed.link
};
})
callback(feeds);
});//.error(function(err) { (disabled at the moment in favour of ajaxError event)
// console.log('Error: ' + err.error);
// });
}
我的客戶端代碼(在另一個JS源文件中):
var options = { logging: true };
myAPI = new Akamanda.Client(options);
var feeds = [];
var articles = [];
function getFeeds()
{
myAPI.getFeeds(function(AkamandaFeeds) {
feeds = AkamandaFeeds;
showFeeds();
});
}
從您發布的代碼中可以看到,您從未實例化Akamanda.Client類型的對象。
var Client = new Akamanda.Client();
要么
var Akamanda.Client = {};
Akamanda.Client.logging = ....
JSBin示例: http : //jsbin.com/ajidig/1/edit
好的,這里有個例子(真實代碼,但非常簡化):
//we wrap our code in a self invoking function so that we don't pollute the global namespace, see http://stackoverflow.com/questions/6715805/self-invoking-functions-javascript for further details
(function(){
//create your object that holds all your function, that are different ways to do this
var Akamanda = {};
//a private function
function ErrorHandler(clientObj) {
this.clientObj = clientObj;
//do whatever with clientObj
this.log = function(){..}
}
//private constructor for clientobj
function Client(options){
..
}
Akamanda.Client = function(){
var newClient = new Client({..});
//setup
Akamanda.ErrorLogging = new ErrorHandler(newClient);
return newClient;
}
//bind our service to the window object to make it accesible
window.Akamanda = Akamanda;
})()
//client
var myAPI = Akamanda.Client();
Akamanda.ErrorLogging.log();
希望這些基本示例對您有所幫助。 如果您需要更多有關Javascript模式的知識,我可以推薦jQuery的作者John Resig撰寫的這本書http://jsninja.com/ 。 根據您想做什么,還有許多類似http://backbonejs.org/的框架可以幫助您解決這些問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.