构造函数的继承
程序员文章站
2022-05-14 10:06:25
...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>构造函数的继承</title>
</head>
<body>
<script>
function Father() {
this.name = '马云'
}
function Son() {
Father.call(this) // 通过 call 将Father构造函数的this 改变为Son的this 这样两个构造函数this指向相同(所以都可以取到name变量)(apply也可实现此操作)
}
var father = new Father()
var son = new Son()
console.log(father, son)
</script>
</body>
</html>