码迷,mamicode.com
首页 > Web开发 > 详细

JS object factory and inherit sample

时间:2014-05-19 22:39:02      阅读:472      评论:0      收藏:0      [点我收藏+]

标签:class   c   code   java   a   javascript   

/*
 * Object factory
 */
function objectFactory(jsonObj){
	function objectEntity(){
	
	}
	if(typeof jsonObj == "object"){
		for(var index in jsonObj){
			objectEntity.prototype[index] = jsonObj[index]; 
		}	
	}
	return objectEntity;
}

var Person = objectFactory({
	pname:‘andy‘,
	sex:‘man‘
});

var person = new Person();
console.info(person+"--"+Person);// [object Object] -- function objectEntity(){}
console.info(person.pname);
console.info(person.sex);
 
objectFactory create object per json obj-jsonObj
create function objectEntity will check whether jsonObj is object, and iterate the json object, set attribute value to
objectEntity
return objectEntity, while Person refer to objectEntity
/*
 * inherit
 */
function inherit(obj,prop){
	function f(){
		
	}
	if(typeof obj=="object"){
		for(var index in obj){
			f.prototype[index] = obj[index]; 
		}
	}else{
		f.prototype = obj.prototype;
		for(var index in prop){
			f.prototype[index] = prop[index]; 
		}
	}
	
	return f;
}

var Animal = inherit({
	type:‘animal‘,
	name:‘animal‘,
	jump:‘jump‘
});

var Dog = inherit(Animal,{
	name:‘i am a dog‘,
	jump:‘dog jumpping‘
});

var dog = new Dog;
console.info(dog.type);
console.info(dog.name);

JS object factory and inherit sample,布布扣,bubuko.com

JS object factory and inherit sample

标签:class   c   code   java   a   javascript   

原文地址:http://www.cnblogs.com/glenblogs/p/3731350.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!