- js 继承剩余的两种方法
- new 关键字 的工作过程
- ES5 构造函数function 与 ES6 类 class
- 函数式编程
es6
class Person {
constructor () {
this.name = 'Cherish'
}
static eat () {
console.log('eat')
}
travel () {
console.log('travel')
}
drink = function () {
console.log('drink')
}
}
Person.eat()
class Man extends Person {
constructor () {
super()
this.name = 'Man'
}
code () {
console.log('code')
}
}
转 es5
"use strict";
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; }; }();
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; };
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; }
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; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var _createClassTemp = 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;
};
}();
function _inheritsTemp(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function, not " + (typeof superClass === "undefined" ? "undefined" : _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;
}
var Person = function () {
function Person() {
_classCallCheck(this, Person);
this.drink = function () {
console.log('drink');
};
this.name = 'Cherish';
}
_createClass(Person, [{
key: "travel",
value: function travel() {
console.log('travel');
}
}], [{
key: "eat",
value: function eat() {
console.log('eat');
}
}]);
return Person;
}();
Person.eat();
var Man = function (_Person) {
_inherits(Man, _Person);
function Man() {
_classCallCheck(this, Man);
var _this = _possibleConstructorReturn(this, (Man.__proto__ || Object.getPrototypeOf(Man)).call(this));
_this.name = 'Man';
return _this;
}
_createClass(Man, [{
key: "code",
value: function code() {
console.log('code');
}
}]);
return Man;
}(Person);
console.log(Man.prototype);
var personA = new Person();
personA.code();