Home > Web Front-end > JS Tutorial > Detailed explanation of JS mixed inheritance

Detailed explanation of JS mixed inheritance

小云云
Release: 2018-03-17 17:06:38
Original
1714 people have browsed it

This article mainly shares with you the detailed explanation of JS mixed inheritance, hoping to help everyone.

<script type="text/javascript">
	window.onload=function(){
//混合继承:原型实现继承+借用构造函数继承
function Person(name,age,gender,wight){
	this.name=name;
	this.age=age;
	this.gender=gender;
	this.wight=wight;
}
Person.prototype.sayHi=function(){
	console.log("欢迎!");	
}
function Student(name,age,gender,wight,score){
	Person.call(this,name,age,gender,wight);//实现属性继承
	this.score=score;
}
Student.prototype=new Person();//实现方法继承
Student.prototype.sleep=function(){
	console.log("请保证充足睡眠!");
}
var stu=new Student("lll",20,"male",150,100);
console.log(stu.name,stu.age,stu.gender,stu.wight,stu.score);
stu.sayHi();
stu.sleep();
var stu2=new Student("222",22,"female",100,110);
console.log(stu2.name,stu2.age,stu2.gender,stu2.wight,stu2.score);
stu2.sayHi();
stu2.sleep();

}
</script>
Copy after login

The above is the detailed content of Detailed explanation of JS mixed inheritance. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template