1. <script>
    2. // 1. 父类
    3. function Father() {
    4. this.type = 'father';
    5. this.names = ['tom', 'kevin'];
    6. }
    7. // 2.给父类Father的原型对象添加getName方法
    8. Father.prototype.getName = function() {
    9. console.log(this.names);
    10. }
    11. // 3.创建父类的实例
    12. var father = new Father();
    13. // console.log(father);
    14. // 2. 子类
    15. function Child() {
    16. }
    17. // 3. 让子类的原型对象等于父类的一个实例, 完成原型链继承
    18. Child.prototype = father;
    19. // 4. 创建一个子类实例
    20. var child = new Child();
    21. console.log(child);
    22. // 5. 创建另外一个实例
    23. var child2 = new Child();
    24. console.log(child2);
    25. // 6. 缺点展示: 给其中一个实例的names添加一个成员
    26. child2.names.push('zs');
    27. console.log('child2.names', child2.names);
    28. console.log('child.names', child.names);
    29. // 不是引用类型,不受影响
    30. child2.type = 'aaaa';
    31. console.log('child2.type', child2.type);
    32. console.log('child.type', child.type);
    33. </script>