JS中如何实现属性和方法的继承

JS中面向对象的实现:

         function Person(name,color){
			this.name = name;
			this.color = color;
		}

		Person.prototype.showName = function(){
				alert(this.name);
		}
		Person.prototype.showColor = function(){
				alert(this.color);
		}

		function Worker(name,color,job,age){
				Person.apply(this,arguments)
				this.job = job;
				this.age = age;
		}
		for(var i in Person.prototype){
				Worker.prototype = Person.prototype;
		}
		Worker.prototype.showAge = function(){
			alert(this.age);
		};
		var workMan = new Worker('sl', 'red','coders',34);
		workMan.showName();  //sl
		workMan.showColor();   //red
		workMan.showAge();   //34

  面向对象继承属性 方法,添加属性和方法

  

posted on 2016-03-05 13:57  金甲  阅读(434)  评论(0)    收藏  举报

导航