主要完成一下几个功能
var Plane = (function(){
function Plane(name){
this.name = name || "一般飞机";
this.blood = 100;
}
return Plane;
})()
function _classCallCheck(_this,_constructor){
if(!(_this instanceof _constructor)){
throw new TypeError(" Class constructor Plane cannot be invoked without 'new'");
}
}
var Plane = (function(){
function Plane(name){
_classCallCheck(this, Plane);
this.name = name || "一般飞机";
this.blood = 100;
}
return Plane;
})()
不通过new的方式创建
通过new的方式创建
//封装原型上的方法和构造函数上的方法
function _createClass(_constructor, _prototypeProperties, _staticProperties){
if(_prototypeProperties){
defineProperties(_constructor.prototype, _prototypeProperties);
}
if(_staticProperties){
defineProperties(_constructor, _staticProperties);
}
}
function defineProperties(target, prop){
//将_prototypeProperties, _staticProperties中的对象拿出来循环
prop.forEach(function(ele){
//diao'yong
Object.defineProperty(target,ele.key,{
value: ele.value,
writable: true,
configurable: true
})
})
}
//检测是否是通过new的方式创建对象
function _classCallCheck(_this,_constructor){
if(!(_this instanceof _constructor)){
throw new TypeError(" Class constructor Plane cannot be invoked without 'new'");
}
}
var Plane = (function(){
function Plane(name){
_classCallCheck(this, Plane);
this.name = name || "一般飞机";
this.blood = 100;
}
_createClass(AttackPlane, [
{
key: "fly",
value: function (){
console.log("fly");
}
}
], [
{
key: "alive",
value: function(){
return true;
}
}
]);
return Plane;
})()
var AttackPlane = (function(Plane){
function AttackPlane(){
Plane.apply(this, Plane);
this.logo = "duyi";
}
_createClass(AttackPlane, [
{
key: "fly",
value: function (){
console.log("fly");
}
}
], [
{
key: "alive",
value: function(){
return true;
}
}
]);
return AttackPlane;
})(Plane)
//继承的方法
function _inherit(sub, sup){
Object.setPrototypeOf(sub.prototype, sup.prototype);
}
//封装原型上的方法和构造函数上的方法
function _createClass(_constructor, _prototypeProperties, _staticProperties){
if(_prototypeProperties){
defineProperties(_constructor.prototype, _prototypeProperties);
}
if(_staticProperties){
defineProperties(_constructor, _staticProperties);
}
}
function defineProperties(target, prop){
//将_prototypeProperties, _staticProperties中的对象拿出来循环
prop.forEach(function(ele){
//diao'yong
Object.defineProperty(target,ele.key,{
value: ele.value,
writable: true,
configurable: true
})
})
}
//检测是否是通过new的方式创建对象
function _classCallCheck(_this,_constructor){
if(!(_this instanceof _constructor)){
throw new TypeError(" Class constructor Plane cannot be invoked without 'new'");
}
}
var Plane = (function(){
function Plane(name){
_classCallCheck(this, Plane);
this.name = name || "一般飞机";
this.blood = 100;
}
_createClass(AttackPlane, [
{
key: "fly",
value: function (){
console.log("fly");
}
}
], [
{
key: "alive",
value: function(){
return true;
}
}
]);
return Plane;
})()
var AttackPlane = (function(Plane){
_inherit(AttackPlane, Plane);
function AttackPlane(){
Plane.apply(this, Plane);
this.logo = "duyi";
}
_createClass(AttackPlane, [
{
key: "fly",
value: function (){
console.log("fly");
}
}
], [
{
key: "alive",
value: function(){
return true;
}
}
]);
return AttackPlane;
})(Plane)
var plane = new Plane("梦想号");
var apPlane = new AttackPlane("歼击机-01");
先看一下刚才所述的情况
此处子类继承的对象就不是父类属性 所以此处需要再做一下处理
//继承的方法
function _inherit(sub, sup){
Object.setPrototypeOf(sub.prototype, sup.prototype);
}
//封装原型上的方法和构造函数上的方法
function _createClass(_constructor, _prototypeProperties, _staticProperties){
if(_prototypeProperties){
defineProperties(_constructor.prototype, _prototypeProperties);
}
if(_staticProperties){
defineProperties(_constructor, _staticProperties);
}
}
function defineProperties(target, prop){
//将_prototypeProperties, _staticProperties中的对象拿出来循环
prop.forEach(function(ele){
//diao'yong
Object.defineProperty(target,ele.key,{
value: ele.value,
writable: true,
configurable: true
})
})
}
//检测是否是通过new的方式创建对象
function _classCallCheck(_this,_constructor){
if(!(_this instanceof _constructor)){
throw new TypeError(" Class constructor Plane cannot be invoked without 'new'");
}
}
var Plane = (function(){
function Plane(name){
_classCallCheck(this, Plane);
this.name = name || "一般飞机";
this.blood = 100;
return {
son: "xixi"
}
}
_createClass(AttackPlane, [
{
key: "fly",
value: function (){
console.log("fly");
}
}
], [
{
key: "alive",
value: function(){
return true;
}
}
]);
return Plane;
})()
var AttackPlane = (function(Plane){
_inherit(AttackPlane, Plane);
function AttackPlane(){
var _this = this;
var that = Plane.apply(_this, Plane);
if(typeof that == "object"){
_this = that;
}
_this.logo = "duyi";
return _this;
}
_createClass(AttackPlane, [
{
key: "fly",
value: function (){
console.log("fly");
}
}
], [
{
key: "alive",
value: function(){
return true;
}
}
]);
return AttackPlane;
})(Plane)
var plane = new Plane("梦想号");
var apPlane = new AttackPlane("歼击机-01");
修改了子类的的构造函数的方法以后,可以成功的解决刚才的问题 效果如下图