JavaScript类

https://zh.javascript.info/class-inheritance#homeobject

class Animal {
  constructor(name) {
    this.name = name;
  }
}
class Rabbit extends Animal {
  constructor(name) {
    super(name);
    this.created = Date.now();
  }
}
let rabbit = new Rabbit("White Rabbit");
alert(rabbit.name);

es6的类继承,通过babel编译

"use strict";

function _inherits(subClass, superClass) {
	if (typeof superClass !== "function" && superClass !== null) {
		throw new TypeError("Super expression must either be null or a function");
	}
    // 设置subClass的prototype
	subClass.prototype = Object.create(superClass && superClass.prototype, {
			constructor: {
				value: subClass,
				writable: true,
				configurable: true
			}
		});
	if (superClass)
		_setPrototypeOf(subClass, superClass);
}

function _setPrototypeOf(o, p) {
	_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
		o.__proto__ = p;
		return o;
	};
	return _setPrototypeOf(o, p);
}

function _createSuper(Derived) {
	var hasNativeReflectConstruct = _isNativeReflectConstruct();
	return function _createSuperInternal() {
		var Super = _getPrototypeOf(Derived),
		result;
		if (hasNativeReflectConstruct) {
			var NewTarget = _getPrototypeOf(this).constructor;
			result = Reflect.construct(Super, arguments, NewTarget);
		} else {
			result = Super.apply(this, arguments);
		}
		return _possibleConstructorReturn(this, result);
	};
}

function _possibleConstructorReturn(self, call) {
	if (call && (typeof call === "object" || typeof call === "function")) {
		return call;
	}
	return _assertThisInitialized(self);
}

function _assertThisInitialized(self) {
	if (self === void 0) {
		throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
	}
	return self;
}

function _isNativeReflectConstruct() {
	if (typeof Reflect === "undefined" || !Reflect.construct)
		return false;
	if (Reflect.construct.sham)
		return false;
	if (typeof Proxy === "function")
		return true;
	try {
		Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
		return true;
	} catch (e) {
		return false;
	}
}

function _getPrototypeOf(o) {
	_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
		return o.__proto__ || Object.getPrototypeOf(o);
	};
	return _getPrototypeOf(o);
}

function _classCallCheck(instance, Constructor) {
	if (!(instance instanceof Constructor)) {
		throw new TypeError("Cannot call a class as a function");
	}
}

let Animal = function Animal(name) {
	_classCallCheck(this, Animal);

	this.name = name;
};

let Rabbit = /*#__PURE__*/ function (_Animal) {
	_inherits(Rabbit, _Animal);

	var _super = _createSuper(Rabbit);

	function Rabbit(name) {
		var _this;

		_classCallCheck(this, Rabbit);
		/** 
			function _createSuper(Derived){
				return function superInner(){
					var Super = _getPrototypeOf(Derived);
					var result = Super.apply(this, arguments);
					return result;
				}
			}
			借用父级的构造函数 Animal,把属性初始化到当前的实例中
			_this = superInner.call(this,name);
			_this.created = Date.now();
			return _this;
		*/
		_this = _super.call(this, name);
		_this.created = Date.now();
		return _this;
	}

	return Rabbit;
}
(Animal);

let rabbit = new Rabbit("White Rabbit"); // 现在好了

alert(rabbit.name); // White Rabbit
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值