<!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>
</body>
</html>
<script>
/**
* 一种思路
* 在原型的基础上增强了对象,返回新的构造函数
*
* 缺点
* 无法传参
* 共享 ·引用类型值· 修改一个全部修改
* 没有用到原型 无法服用
*/
function Fun(origin) {
let t = Object.create(origin);
t.name = 'my'
t.my = function () {
console.log(this);
return `this.name is ${this.name}`
};
return t;
}
let origin = {
name: "前端伪大叔",
books: ["html", "css", "JavaScript"]
};
let fun = Fun(origin);
let fun1 = Fun(origin);
// 共享引用类型
fun.books.push('vue')
fun.books.push('react')
console.log(fun.books); // (5) ["html", "css", "JavaScript", "vue", "react"]
console.log(fun1.books); // (5) ["html", "css", "JavaScript", "vue", "react"]
</script>