如何在私有方法中的javascript模块模式中访问它?
在使用javascript模块模式的私有方法时,如何访问该对象 我不明白这是怎么回事,我还在努力学习如何在私有方法中的javascript模块模式中访问它?,javascript,Javascript,在使用javascript模块模式的私有方法时,如何访问该对象 我不明白这是怎么回事,我还在努力学习 var o = o || {}; o.Assets = (function() { "use strict"; function privateFunc() { // how do I get this.assetQ? } function Assets(assetQ) { this.init(assetQ); };
var o = o || {};
o.Assets = (function() {
"use strict";
function privateFunc() {
// how do I get this.assetQ?
}
function Assets(assetQ) {
this.init(assetQ);
};
Assets.prototype.assetQ = [];
Assets.prototype.cache = [];
Assets.prototype.callback = false;
/*
Initialize the assets
*/
Assets.prototype.init = function(assetQ) {
if (assetQ) {
this.assetQ = assetQ;
}
};
return Assets;
})();
从
Assets
方法调用privateFunc
时,使用privateFunc.call(this)
然后在privateFunc
内部使用this.assetQ
function privateFunc() {
console.log(this.assetQ);
}
如果私有函数采用参数,则可以在中传递它们。通常在上下文参数之后调用:
privateFunc.call( this, 1, 2, 3 );
function privateFunc( a, b, c ) {
//a is 1, b is 2 etc..
}
我怎样才能访问这个?“这”是什么?谢谢你,我还在学习,这对我很有帮助:)使用模块化模式时通常是这样做的吗?@user2251919当然还有其他选择,它们也同样有效。坚持一种方式是最重要的。
privateFunc.call( this, 1, 2, 3 );
function privateFunc( a, b, c ) {
//a is 1, b is 2 etc..
}