Tuesday, June 8, 2010

Exporting module functions to window and "Object doesn't support this property or method"

Try to gues what you get from each sample call in MSIE 7.0 ?

//-------------------------------------------------------------------------
// sample1: jQuery style of export
(function(window, undefined) {
    var query = function() {
        throw new Error("hop");
    }
    window.query = query;

})(window);


window.onload = function() {
    try {
        query();
    } catch (ex) {
        //???
        alert(ex.message);
    }
}
//-------------------------------------------------------------------------// sample1: exporting to "custom namespace" not window
var ns = {};
(function(window, undefined) {
    var query = function() {
        throw new Error("hop");
    }
    window.query = query;

})(ns);

window.onload = function() {
    try {
        ns.query();
    } catch (ex) {
        //???
        alert(ex.message);
    }
}
//-------------------------------------------------------------------------// plain old "global scope method"

var query = function() {
    throw new Error("hop");
}
window.onload = function() {

    try {
        query();
    } catch (ex) {
        //???
        alert(ex.message);
    }
}

Final fix ? export,import


(function(window, undefined) {
    var query = function() {
        throw new Error("hop");
    }
    window.query = query;
})(window);


window.onload = function() {
    var query = window.query;
    try {
        query();
    } catch (ex) {
        //???
        alert(ex.message);
    }
}

No comments:

Post a Comment