1. function object(){
    2. function F() {}
    3. F.prototype = proto;
    4. return new F();
    5. }
    6. function _inherits(Child, Parent){
    7. // Object.create
    8. Child.prototype = Object.create(Parent.prototype);
    9. // __proto__
    10. // Child.prototype.__proto__ = Parent.prototype;
    11. Child.prototype.constructor = Child;
    12. // ES6
    13. // Object.setPrototypeOf(Child, Parent);
    14. // __proto__
    15. Child.__proto__ = Parent;
    16. }
    17. function Parent(name){
    18. this.name = name;
    19. }
    20. Parent.sayHello = function(){
    21. console.log('hello');
    22. }
    23. Parent.prototype.sayName = function(){
    24. console.log('my name is ' + this.name);
    25. return this.name;
    26. }
    27. function Child(name, age){
    28. // 相当于super
    29. Parent.call(this, name);
    30. this.age = age;
    31. }
    32. _inherits(Child, Parent);
    33. Child.prototype.sayAge = function(){
    34. console.log('my age is ' + this.age);
    35. return this.age;
    36. }
    37. var parent = new Parent('Parent');
    38. var child = new Child('Child', 18);