• js 继承剩余的两种方法
    • new 关键字 的工作过程
    • ES5 构造函数function 与 ES6 类 class
    • 函数式编程

    es6

    1. class Person {
    2. constructor () {
    3. this.name = 'Cherish'
    4. }
    5. static eat () {
    6. console.log('eat')
    7. }
    8. travel () {
    9. console.log('travel')
    10. }
    11. drink = function () {
    12. console.log('drink')
    13. }
    14. }
    15. Person.eat()
    16. class Man extends Person {
    17. constructor () {
    18. super()
    19. this.name = 'Man'
    20. }
    21. code () {
    22. console.log('code')
    23. }
    24. }

    转 es5

    1. "use strict";
    2. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
    3. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
    4. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
    5. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
    6. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
    7. var _createClassTemp = function () {
    8. function defineProperties(target, props) {
    9. for (var i = 0; i < props.length; i++) {
    10. var descriptor = props[i];
    11. descriptor.enumerable = descriptor.enumerable || false;
    12. descriptor.configurable = true;
    13. if ("value" in descriptor) descriptor.writable = true;
    14. Object.defineProperty(target, descriptor.key, descriptor);
    15. }
    16. }
    17. return function (Constructor, protoProps, staticProps) {
    18. if (protoProps) defineProperties(Constructor.prototype, protoProps);
    19. if (staticProps) defineProperties(Constructor, staticProps);
    20. return Constructor;
    21. };
    22. }();
    23. function _inheritsTemp(subClass, superClass) {
    24. if (typeof superClass !== "function" && superClass !== null) {
    25. throw new TypeError("Super expression must either be null or a function, not " + (typeof superClass === "undefined" ? "undefined" : _typeof(superClass)));
    26. }
    27. subClass.prototype = Object.create(superClass && superClass.prototype, {
    28. constructor: {
    29. value: subClass,
    30. enumerable: false,
    31. writable: true,
    32. configurable: true
    33. }
    34. });
    35. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
    36. }
    37. var Person = function () {
    38. function Person() {
    39. _classCallCheck(this, Person);
    40. this.drink = function () {
    41. console.log('drink');
    42. };
    43. this.name = 'Cherish';
    44. }
    45. _createClass(Person, [{
    46. key: "travel",
    47. value: function travel() {
    48. console.log('travel');
    49. }
    50. }], [{
    51. key: "eat",
    52. value: function eat() {
    53. console.log('eat');
    54. }
    55. }]);
    56. return Person;
    57. }();
    58. Person.eat();
    59. var Man = function (_Person) {
    60. _inherits(Man, _Person);
    61. function Man() {
    62. _classCallCheck(this, Man);
    63. var _this = _possibleConstructorReturn(this, (Man.__proto__ || Object.getPrototypeOf(Man)).call(this));
    64. _this.name = 'Man';
    65. return _this;
    66. }
    67. _createClass(Man, [{
    68. key: "code",
    69. value: function code() {
    70. console.log('code');
    71. }
    72. }]);
    73. return Man;
    74. }(Person);
    75. console.log(Man.prototype);
    76. var personA = new Person();
    77. personA.code();