1. <script>
    2. // 1. 父类构造器
    3. function Father(name) {
    4. this.name = name;
    5. this.list = [1, 2, 3];
    6. this.say = function() {
    7. console.log('hello');
    8. }
    9. }
    10. // Child的实例无法继承Father原型上的属性或方法
    11. Father.prototype.aaa = function() {
    12. console.log('aaa');
    13. }
    14. // 2. 子类
    15. function Child(name) {
    16. // 3. 借用父类的构造器
    17. Father.call(this, name);
    18. }
    19. // 4. 创建实例1
    20. var child = new Child('小王');
    21. // 5. 创建实例2
    22. var child2 = new Child('小红');
    23. child2.list.push(4);
    24. console.log('child2', child2.list);
    25. console.log('child', child.list);
    26. // 6. 缺点展示: 方法会创建很多遍
    27. console.log(child.aaa); // undefined,方法只能定义在父类的构造函数里
    28. console.log(child.say === child2.say); // false, 所以每个实例都会创建一遍方法
    29. </script>