1. <script>
    2. // 1.创建父类
    3. function Father(name, age) {
    4. this.name = name;
    5. this.age = age;
    6. this.list = [1, 2, 3];
    7. }
    8. // 2.给父类的原型对象添加say方法
    9. Father.prototype.say = function() {
    10. console.log('hello');
    11. }
    12. // 3.创建子类
    13. function Child(name, age) {
    14. // 4.借用父类构造函数
    15. Father.call(this, name, age);
    16. }
    17. // 5.让子类的原型对象等于父类的一个实例,这样子类就继承了父类的所有属性和方法,new Father时可以传参也可以不传
    18. Child.prototype = new Father('张无忌', 50);
    19. // 6.创建子类实例
    20. var child = new Child('张三', 18);
    21. console.log('child', child);
    22. var child2 = new Child('李四', 20);
    23. console.log('child', child2);
    24. console.log(child.list === child2.list); //false,引用类型属性不会相互影响
    25. </script>