我有一个模块化的JS项目,我被给予调整。它使用原型继承而不是类。以下是有问题的代码:
hgManager.js中的构造函数:
export function HGManager() {
this.USER_ID = getURLParameter( "usr" ),
this.GAME_ID = getURLParameter( "game" )
};
hgManager.js中的getData():
getData: function(a, b) {
var c = this.API + "records/" + this.USER_ID + "/" + this.GAME_ID;
this.xhrLoad( "GET", c, a, b )
},
hdManager.js中的xhrLoad():
xhrLoad: function(a, b, c, d, e) {
var f = new XMLHttpRequest;
f.open(a, b, true),
e && f.setRequestHeader("Content-type", "application/json");
var g = this;
f.onload = function() {
if (4 == f.readyState && f.status >= 400 && f.status <= 599) { return d.call(g, f);
}
else {
var a = JSON.parse( f.responseText ).response;
return c.call(g, a, f)
}
},
f.onerror = function() {
return d.call(g, f)
},
e ? f.send( JSON.stringify( e ) ) : f.send()
}
调用hgManager.getData()的函数:
loadPlayerData: function() {
var a = this;
this.game.hgManager.getData(
function( c ) { //param 1
if ( null === c ) {
return void console.log( "DataManager: Invalid response." ); //if there is no playerData
}
var d = JSON.parse( c.record );
void 0 === d || null === d || void 0 === d.selectedCharacter ? (console.log("DataManager: No data on backend, looking for data on local storage."), d = a._getLocalStorageData(), null !== d ? (console.log("DataManager: Data on localstorage found. Saving this to backend."), a.game.playerData = d) : console.log("DataManager: No data on localstorage. Saving default data to backend."), a.savePlayerData()) : console.log("DataManager: Data loaded from backend.");
var e = new Date,
f = e.getFullYear() + "-" + e.getMonth();
d.lastMonthPlayed != f && (d.lastMonthPlayed = f, d.loyaltyPoints = [], console.log("DataManager: New month, reset loyalty points.")),
a.game.playerData = d,
a.game.hasShownLoyaltyMessage = a.game.playerData.loyaltyPoints.length > 0,
a.game.hasShownPortalMessage = 9 == a.game.playerData.portalPieces.length
},
function() { //param 2
console.log("DataManager: Error loading user data"),
data = a._getLocalStorageData(),
null !== data ? (console.log("DataManager: Data on localstorage found."), a.game.playerData = data) : console.log("DataManager: No data on localstorage.")
}
)
},
让我失望的代码是return c.call(g, a, f)
中的xhrLoad()
,以及loadPlayerData()
的相应第一个参数函数。
参数&#39; c&#39; this.game.hgManager.getData(function( c ) {
来自哪里?显然没有在这个范围内定义,所以我想它是call()
的结果?
loadPlayerData()
如何阅读范围内未定义的内容?
鉴于函数this.game.hgManager.getData( function(c)
,我们为什么要重新分配父对象并调用getData()
?意图是什么?
答案 0 :(得分:1)
处理a, b, c
之类的变量非常困难,尤其是当它们在不同范围内表示不同的事物时。
但是,让我们尝试关注代码并重命名args以添加一些意思:
xhrLoad: function(method, target, callbackSuccess, callbackError, e/* idk what is it*/) {}
getData: function(callbackSuccess, callbackError) {
var target = this.API + "records/" + this.USER_ID + "/" + this.GAME_ID;
this.xhrLoad( "GET", target, callbackSuccess, callbackError )
},
this.game.hgManager.getData(
function( response ) { //param 1 callbackSucess
if ( null === response ) {
return void console.log( "DataManager: Invalid response." ); //if there is no playerData
}
},
function() { //param 2 callbackError
//
}
现在更容易理解。
getData()
接受两个回调函数作为参数 - 一个用于成功响应,一个用于错误。首先必须接受回应作为论据。这是来自c
的{{1}},它就在这里定义。由于它是函数的参数,因此无需在全局范围内定义它。
看来,这里与课程无关。这都是关于将函数作为参数传递的。