日期:2014-05-16  浏览次数:20358 次

jQuery构造求解
话说jQuery的代码我没看太懂,请各位高人指点一下,下面代码是我依葫芦画瓢瞎写的,似乎能做到fm1和fm2的区别对待。但是还是不理解为什么我把this.settings移动到跟version平级的位置,就不行,那个this难道不是fn吗?请详解,困惑!

JScript code

(function (window) {
    var floatMenu = function (settings) {
        return new floatMenu.fn.init(settings);
    };
    floatMenu.fn = floatMenu.prototype = {
        version: "1.0",
        init: function (settings) {
            this.settings = {};
            util.extends(this.settings, settings);
        },
        load: function () {
            return this.settings.hello;
        }
    }
    floatMenu.fn.init.prototype = floatMenu.fn;

    var util = {
        extends: function (collection, collectionOverride) {
            if (collectionOverride && collection) {
                for (var key in collectionOverride) {
                    collection[key] = collectionOverride[key];
                }
            }
        }
    };

    if (window) {
        window.$fm = window.floatMenu = floatMenu;
    }
})(window);

var fm1 = $fm({ "hello": "world2" });
var fm2 = $fm({ "hello": "world3" });
alert(fm1.load());
alert(fm2.load());
alert(fm1.load());
alert(fm2.load());



------解决方案--------------------
this指的是当前对象或者当前方法

JScript code

function (settings) {
            this.settings = {};
            util.extends(this.settings, settings);
}