开发者

Alternate way of defining object? Object literal works but has redundancies

Okay so I have made myself a small object to assess the whole loading jquery with a fallback thing (and have employed it to loading all my js files, infact), using JSON:

<script type="text/javascript">

/* Library loading object */

var ScriptLoader = {
    //values
    head_tag : document.getElementsByTagName('head')[0],
    fallback : false,

    //functions
    createScript: function(script_path) {
        var script_tag = document.createElement('script');
        script_tag.type = 'text/javascript';
        script_tag.src = script_path;
        return script_tag;
    },

    addToHead : function(tag) {
        ScriptLoader.head_tag.appendChild(tag);
    },

    addLoadEvent : function(func) {
        var prev = window.onload;

        if (typeof window.onload !== 'function') {
            window.onload = func;
        } else {
            window.onload = function() {
                prev();
                func();
            }
        }
    }
};


//Loading jquery, modernizr, and my js files when window is ready.

ScriptLoader.addLoadEvent(function() {
    /* Load from jquery's cdn for speed + auto latest download */
    var scripts = ['http://code.jquery.com/jquery-latest.min.js',
        'scripts/modernizr.js',
        'scripts/script.js'],
            idx = 0;
    for (; idx < scripts.length; idx++) {
        ScriptLoader.addToHead(ScriptLoader.createScript(scripts[idx]));
    }
});

//jquery fallback
ScriptLoader.addLoadEvent(function() {
    if (typeof window.jQuery === undefined) {
        ScriptLoader.fallback = true;
        ScriptLoader.addToHead(ScriptLoader.createScript('scripts/jquery-mini-1.6.2.js'));
    }
});

//grab info
(function() {
    setTimeout(function() {
        console.log('jquery loaded: ' + (window.jQuery !== undefined));
        console.log('used fallback: ' + ScriptLoader.fallback);
    }, 1000);
})();

Right now, this is fully working, but notice the constant calls to my object's name? Is there a way to simply just use this instead of that? I kind of get the whole functional-level scope only thing, and also realize that this gets assigned to the nearest enclosing function, but I'm not crystal clear on that..

I also breifly read o开发者_运维百科n but don't fully understand closures..

Thanks :)


No because this refers to the instance of the object, not the object template.

Closures are simply returned functions from within functions which can then be assigned to new variables and executed as if they were parent functions. The closure retains the scope of its original function parent even though there is no mention of it by its new assignment. This is why closures are prone to memory leaks, because ECMAScript's garbage collector has no scope of the closure assignments.

function foo(a,b){
  var c = 1;
  return function(c){
    return a + b + c;
  }
}

var bar = foo(3,5); // bar is now a closure

alert(bar(2)); // 10
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜