123456789101112131415161718192021222324252627282930313233343536 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <title>Document</title>
- </head>
- <body>
- <script>
- /**
- * 寄生式继承
- * 基于原型式继承 就是在原型式继承的基础上添加方法
- */
- let father = {
- name: "LiLi",
- age: 18,
- getColor: function () {
- return this.age;
- },
- };
- function Way(methods) {
- let obj = Object.create(methods);
- obj.getName = function() {
- return this.name;
- }
- return obj;
- }
- let child = Way(father);
- console.log(child.getColor());
- console.log(child.getName());
- </script>
- </body>
- </html>
|