自己动手-Javascript面向对象-继承

 function Person(){
	this.name="haha";
	this.sext="boy";
	this.getName=function(){
		alert(this.name);
	}
	this.sayHello=function(){
		alert("Person hi");
	};
};
function Student(){

};

Student.prototype=new Person();

Student.prototype.sayHello=function(){
	alert("Student hi");
};

var student1=new Student();
var student2=new Student();

student1.sayHello();
student2.sayHello();

alert("student1的sayHello的方法被修改");

student1.sayHello=function(){
	alert("我是student1 hi");
}

student1.sayHello();
student2.sayHello();

 

-------------------------------(华丽分割线)-----------------------------------------------------------------

 

 

function Person(){
	this.name="haha";
	this.sext="boy";
	this.getName=function(){
		alert(this.name);
	}
	this.classroom="Person classroom"
};
function Student(){
};

Student.prototype=new Person();

Student.prototype.classroom="Student room";

var student1=new Student();
var student2=new Student();

alert("student1-classroom-->"+student1.classroom);
alert("student2-classroom-->"+student2.classroom);

alert("开始修改");
student1.classroom="这是student1 room";

alert("student1-classroom-->"+student1.classroom);
alert("student2-classroom-->"+student2.classroom);

 

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