代码拉取完成,页面将自动刷新
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function Parent(name) {
this.name = name || '干饭人';
this.run = function () {
console.log(this.name + '干饭');
}
}
Parent.prototype.say = function(){
console.log('我是' + this.name);
}
// 在子类构造函数中通用 call() 调用父类型构造函数
function Child(name) {
//call改变指向。 new关键字实例化的对象 this现在是指向child1
Parent.call(this, name);
}
let son = new Child('到点了?'); //'到点了?'作为参数传给父类
son.run(); // 到点了?干饭
son.say(); // Uncaught TypeError: child1.say is not a function
/*
优点:1.避免了引用类型的属性被所有实例共享
2.可以在Child中的向Parent传参数
缺点:1.只能继承父类的实例属性和方法,不能继承原型属性和方法,调用会报错
2.无法实现函数复用,每次创建实例都会创建一遍
*/
</script>
</body>
</html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。