Babel compile: class inheritance

 

Before compilation

 

// 父类
class Fruit {
    static nutrition = "vitamin"
    static plant() {
        console.log('种果树');
    }
    name;
    constructor(name) {
        this.name = name;
    }
    hello() {
        console.log(this.name);
    }
}

// 子类
class Mongo extends Fruit {
    constructor(name, level) {
        super(name);
        this.level = level;
    }
    eat() {
        console.log(super.name, this.name);
    }
}

 

Compiled

 

"use strict" ; 

// Get the type of 
function _typeof (obj) {
     // operating environment natively supports the Symbol 
    IF ( typeof the Symbol === "function" && typeof Symbol.iterator === "Symbol" ) { 
        _typeof = function _typeof ( obj) {
             return  typeof obj; 
        }; 
    } 
    // analog implementation, the Symbol 
    the else { 
        _typeof = function _typeof (obj) {
             return obj &&
                 typeof the Symbol === "function" &&
                obj.constructor === Symbol && 
                obj ! == Symbol.prototype
                 ? "Symbol" 
                : typeof obj; 
        }; 
    } 
    return _typeof (obj); 
} 

/ * * 
 * may call the parent class constructor returns a created thing 
 * @param {Object} self - context 
 * @param {Object} call - call the parent constructor to create a transaction (object / function) 
 * / 
function _possibleConstructorReturn (Self, call) {
     // syntax rules: constructors using the new operator when the operator call can only return the object / function 
    IF (call && (_typeof (call) === "Object" || typeof call === "function" )) {
         returnCall; 
    } 
    return _assertThisInitialized (Self); 
} 

// assertion context has been initialized 
function _assertThisInitialized (Self) {
     // do not call super initial context, Throws 
    IF (Self === void 0 ) {
         the throw  new new a ReferenceError (
             "the this hasn 'T been initialised - Super () has been Not Called " 
        ); 
    } 
    return Self; 
} 

// read attribute 
function _GET (target, property, Receiver) {
     // support the Reflect 
    IF ( typeof the Reflect ==!" undefined " && Reflect.get) {
        _get = Reflect.get;
    } 
    // 模拟Relect
    else {
        _get = function _get(target, property, receiver) {
            var base = _superPropBase(target, property);
            if (!base) return;
            var desc = Object.getOwnPropertyDescriptor(base, property);
            if (desc.get) {
                return desc.get.call(receiver);
            }
            return desc.value;
        };
    }
    return_GET (target, Property, Receiver || target); 
} 

// look up the chain along the prototype, until it finds the first prototype object has the attributes (i.e., not obtained by the attribute inheritance) 
function _superPropBase (Object, Property) {
     the while ( ! Object.prototype.hasOwnProperty.call (Object, Property)) { 
        Object = _getPrototypeOf (Object);
         IF (Object === null ) BREAK ; 
    } 
    return Object; 
} 

// reading the __proto__ 
function _getPrototypeOf (O) { 
    _getPrototypeOf = Object.setPrototypeOf
         ? Object.getPrototypeOf
        : Function _getPrototypeOf (O) {
             return O .__ proto__ || Object.getPrototypeOf (O); 
        }; 
    return _getPrototypeOf (O); 
} 

// inherited 
function _inherits (subClass, superClass) {
     IF ( typeof ! SuperClass == "function" ! && superClass == null ) {
         the throw  new new TypeError ( "Super expression the MUST BE either null or function a" ); 
    } 
    // inherited member method: prototype sub constructor inherit the parent constructor the prototype 
    subClass.prototype = Object. Create (superClass && superClass.prototype, {
        constructor: {value: subClass, Writable: to true , Configurable: to true } 
    }); 
    // inherited static properties, static methods: __proto__ sub constructor is the parent constructor 
    IF (superClass) _setPrototypeOf (subClass, superClass); 
} 

// set the __proto__ 
function _setPrototypeOf (O, P) { 
    _setPrototypeOf = 
        Object.setPrototypeOf ||
         function _setPrototypeOf (O, P) { 
            O .__ proto__ = P;
             return O; 
        }; 
    return _setPrototypeOf (O, P); 
} 

function _instanceof(left, right) {
    if (
        right != null &&
        typeof Symbol !== "undefined" &&
        right[Symbol.hasInstance]
    ) {
        return !!right[Symbol.hasInstance](left);
    } else {
        return left instanceof right;
    }
}

function _classCallCheck(instance, Constructor) {
    if (!_instanceof(instance, Constructor)) {
        throw new TypeError("Cannot call a class as a 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);
    }
}

function _createClass(Constructor, protoProps, staticProps) {
    if (protoProps) _defineProperties(Constructor.prototype, protoProps);
    if (staticProps) _defineProperties(Constructor, staticProps);
    return Constructor;
}

function _defineProperty(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}

var Fruit =
    /*#__PURE__*/
    (function () {
        _createClass(Fruit, null, [
            {
                key: "plant",
                value: function plant() {
                    console.log("种果树");
                }
            }
        ]);

        function Fruit(name) {
            _classCallCheck(this, Fruit);

            _defineProperty(this, "name", void 0);

            this.name = name;
        }

        _createClass(Fruit, [
            {
                key: "hello",
                value: function hello() {
                    console.log(this.name);
                }
            }
        ]);

        return Fruit;
    })();

_defineProperty(Fruit, "nutrition", "vitamin");

var Mongo =
    /*#__PURE__*/
    (function (_Fruit) {
        _inherits(Mongo, _Fruit);

        function Mongo(name, level) {
            var _this;

            _classCallCheck(this, Mongo);

            _this = _possibleConstructorReturn(
                this,
                _getPrototypeOf(Mongo).call(this, name)
            );
            _this.level = level;
            return _this;
        }

        _createClass(Mongo, [
            {
                key: "eat",
                value: function eat() {
                    console.log(
                        _get(_getPrototypeOf(Mongo.prototype), "name", this),
                        this.name
                    );
                }
            }
        ]);

        return Mongo;
    })(Fruit);

 

Guess you like

Origin www.cnblogs.com/sea-breeze/p/11610541.html