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

Javascript基础一(apply, call, arguments, prototype)
//javascript: apply, call, arguments的概念
var Class = function(){
	var klass = function(){
		//apply: http://www.cnblogs.com/fighting_cp/archive/2010/09/20/1831844.html
		//arguments: http://www.cnblogs.com/Fskjb/archive/2011/10/27/2227111.html
		this.init.apply(this,arguments);
	};
	klass.prototype.init = function(){};
	//给"类库"添加方法
	klass.fn = klass.prototype;
	klass.extend = function(obj){
		var extended = obj.extended;
		for(var i in obj){
			//给类添加属性
			klass[i] = obj[i];
			//给实例添加属性
			//klass.fn[i] = obj[i];
		}
		if(extended)extended(klass);
	}
	
	return klass;
}

var Person = new Class();

Person.prototype.init = function(){
	alert(arguments[0] + arguments[1]);
	this.breath();
};

//给类添加函数
Person.eat = function(id){
	alert("find");
}

Person.fn = Person.prototype;
Person.fn.breath = function(){
	alert("breath");
}

Person.extend({
	run: function(){
		alert("run");
	}
});

var person = new Person(1,2);
person.run();

?