javascript继承

<script type="text/javascript">
/*
 *   继承的最佳实践
 *   对象冒充继承属性,原型继承方法
 */

function Star(){
	this.type = 1;
	this.name = 'star';
}
Star.prototype.getName = function(){
	return this.name;
}

function Sun(){
	Star.call(this);
	this.type = 2;
	this.age = 10000;
}
Sun.prototype = new Star();
Sun.prototype.getAge = function(){
	return this.age;
}

var s = new Sun();
show(s);




function show(obj){
	var h = '';
	for(var i in obj){
		h += i+': '+obj[i]+'\n';
	}
	alert(h);
}




</script>
 

你可能感兴趣的:(JavaScript,prototype,sun)