JS原型扩展和函数继承

时间:2022-12-25 14:34:13
<html>
<head>
<meta charset="UTF-8">
<title>原型扩展和函数继承</title>
</head>
<body>
<script type="text/javascript">
// 定义了Person类
var Person = function(name){
this.name = name;
this.say = function(content){
console.log(
this.name + " say: " + content);
}
}
// 实例化
var person = new Person("lay");
// 调用函数
person.say("I'm a person");
// 定义了Student类
var Student = function(name){
// 调用构造函数,继承Person类
Person.call(this, name);
}
// 实例化
var student = new Student("marry");
student.say(
"I'm a student");
// 原型扩展函数
Student.prototype.jump = function(){
console.log(
"I'm jumping...");
}
// 调用函数
student.jump();
</script>
</body>
</html>