简体   繁体   English

与此相关的Javascript模块模式范围

[英]Javascript module pattern scope with this

I was developing using module pattern, and was wondering why I can't acces to the module scope using this. 我正在使用模块模式进行开发,并且想知道为什么我不能使用此模块范围。 Maybe I'm wrong with the understanding of the revealing module pattern. 也许我对显示模块模式的理解是错误的。

Here is the code I use : 这是我使用的代码:

var BLOG = window.BLOG || {};

BLOG.Module = (function(){

    var 
        _this = this,
        _hasLoaded = false;


    function init(){
        console.log(_this); // Logs the Window object

    }


    function _privateMethod(){
        $.ajax({
            url: 'lazyload/lazyload.html',
            success : function( data ){
                // _hasLoaded = true; // I want to access the variable in my module, How do I refer to it? 
            }

        });
    }


    return {
        init : init
    };

})();

this is determined by how a function is called. this取决于函数的调用方式。 If it's called directly, not through an object property (as your outer scoping function is), within that call this will be the global object in loose mode ( undefined in strict mode). 如果它的直接调用,而不是通过一个对象属性(如你的外作用域功能),该呼叫内this将是宽松模式(全局对象undefined严格模式)。 On browsers, that's the window object. 在浏览器中,这就是窗口对象。

You wouldn't normally use this to try to refer to things within that outermost scoping function (for this reason). 通常,您不会用this来尝试引用最外层作用域函数中的内容(因此)。

If something did this: 如果这样做:

BLOG.Module.init();

...then within the call to init , this (not _this ) would refer to Module and you could refer to other properties on the object you create at the end of your outermost scoping function (there aren't any others currently, just init ). ...然后在对init的调用中, this (不是_this )将引用Module并且您可以引用在最外层作用域函数末尾创建的对象上的其他属性(当前没有任何其他属性,仅是init )。


Re your edit: 重新编辑:

var 
    _this = this,
    _hasLoaded = false;

// ...

function _privateMethod(){
    $.ajax({
        url: 'lazyload/lazyload.html',
        success : function( data ){
            // _hasLoaded = true; // I want to access the variable in my module, How do I refer to it? 
        }

    });
}

Just uncomment that line: 只需取消注释该行:

_hasLoaded = true;

This is because both _privateMethod and any ajax success handlers created as a result of calling _privateMethod are closures over the variables defined within your outermost scoping function. 这是因为_privateMethod和由于调用_privateMethod而创建的任何ajax成功处理程序都是对最外层作用域函数中定义的变量的闭包 So you just refer to them directly. 因此,您只需要直接引用它们即可。

If this use of the word "closure" is unfamiliar, don't worry, closures are not complicated . 如果对“ closure”一词的这种用法不熟悉,请不用担心, closure并不复杂


Side note: This is an odd construct: 旁注:这是一个奇怪的构造:

var BLOG = window.BLOG || {};

...as it mixes code requiring that it be at global scope with code that doesn't require that it be at global scope. ...因为它混合了要求在全局范围内的代码和不需要在全局范围内的代码。 It's entirely functional, it's just a bit odd. 它完全功能正常,有点奇怪。 I'd probably go one way or the other: 我可能会选择一种方式:

// Requires that it's at global scope (and yes, this does work)
var BLOG = BLOG || {};

or 要么

// Creates a global whether it's at global scope or not
window.BLOG = window.BLOG || {};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM