算是个简单的js模板吧,记录下来以后会常用

var isJqueryObj = function(obj) {
    return obj instanceof jQuery;
};
/**
 * 构造函数
 * @param {object} opts
 */
var WebDisk = function(opts) {
    opts = $.extend({
        "element": null //容器可以是JQ对象或样式名
    }, opts || {});
    this.opts = opts;
    if (isJqueryObj(opts.element)) {
        this.element = opts.element;
    } else {
        this.element = $(opts.element);
    }
    this.init(); //初始化
};
$.extend(WebDisk.prototype, {
    init: function() {
        var elEl = this.element;
        var opts = this.opts;
        this.bindEvents(); //事件绑定
    },
    bindEvents: function() {
        var elEl = this.element;
        elEl.on('click', '.xxx', function() {
            $(this).show();
        });
    },
    destroy: function() {
        
    }
});

JS类模拟练习

Simple JavaScript Inheritance可以给Class添加extend方法,但是他那代码目前还是不能太理解,如果不添加extend方法的话写一个类就很简单了,下面这个应该十分容易理解了:

/* 
 Created on : 2013-11-27, 11:57:35
 Author     : liuxf
 */
var Class = function() {
    var klass = function() {
        this.init.apply(this, arguments);
    };
    klass.prototype.init = function() {
    };
    return klass;
};
var Person = new Class;
Person.prototype.init = function(opts) {
    opts = $.extend({
        "element": 'box'
    }, opts || {});
    this.opts = opts;
    this.element = $(opts.element);
    this.render();
};
Person.prototype.render = function() {
    var elEl = $(this.element);
    var opts = this.opts;
    elEl.html(opts.name + ',' + opts.age);
};
/**
 * 开始执行脚本
 */
$(function() {
    var person = new Person({
        element: '.mybox2',
        name: '李四',
        age: 55
    });
});

使用Simple Inheritance来创建自己的类

最近一直再研究类的写法。因为经常做一些可复用,可扩展的方法。之前一直使用别人包装好的组件,比如玉伯开发的Alare组件,用的很舒服啊~~
但是不是什么项目都要使用那么复杂的组件吧,如果用JQ自己写一个呢,JQ好像还没有类的包装,可能以后会有吧。研究了半天发现了一个好东西,就是John Resig写的 Simple JavaScript Inheritance。
真是完美的解决了我的需求,而且代码也很少,下面就放上我使用的代码示例

(function() {
	/*
	 * Simple Inheritance
	 */
	var initializing = false;
	var fnTest = /xyz/.test(function() {
		xyz;
	}) ? /\b_super\b/ : /.*/;
	this.Class = function() {};
	Class.extend = function(prop) {
		var _super = this.prototype;
		initializing = true;
		var prototype = new this();
		initializing = false;
		for (var name in prop) {
			prototype[name] = typeof prop[name] == "function" &&
				typeof _super[name] == "function" && fnTest.test(prop[name]) ?
				(function(name, fn) {
				return function() {
					var tmp = this._super;
					this._super = _super[name];
					var ret = fn.apply(this, arguments);
					this._super = tmp;
					return ret;
				};
			})(name, prop[name]) :
				prop[name];
		}

		function Class() {
			if (!initializing && this.init)
				this.init.apply(this, arguments);
		}
		Class.prototype = prototype;
		Class.constructor = Class;
		Class.extend = arguments.callee;
		return Class;
	};
})();

$(function() { 
	
	//定义一个Box的类
	var Box = Class.extend({
		init: function(opts) {
			opts = $.extend({
				"element": 'box'
			}, opts || {});
			this.opts = opts;
			this.element = $(opts.element);
			this.render();
		},
		render: function() {
			var elEl = this.element;
			elEl.html(this.opts.name + ',' + this.opts.age);
			this.show();
		},
		show: function() {
			var elEl = this.element;
			elEl.show();
		}
	});

	//实例1 - 直接用new来实例化Box
	var mybox = new Box({
		element: '.mybox',
		name: '张三',
		age: 15
	});

	//实例2 - 先扩展,再用new来实例化Box
	var mybox2 = Box.extend({
		init: function(opts) {
			this.opts = opts;
			this.element = $(opts.element);
			this.render();
			this.event();
		},
		hide: function() {
			var elEl = this.element;
			elEl.hide();
		},
		event: function() {
			var elEl = this.element;
			var that = this;
			elEl.on('click', function() {
				that.hide();
			})
		}
	});
	new mybox2({
		element: '.mybox2',
		name: '李四',
		age: 55
	});

})