85度c蛋糕房网站系统建设,专门做自助游的网站,网站窗口建设,大型网站团队人数#xff08;对于不屈不挠的人来说#xff0c;没有失败这回事。——俾斯麦#xff09; class 
相关链接 
MDN链接 有关类的详细描述 关于构造函数#xff0c;原型和原型链的说明 
类的概述 
类是用于创建对象的模板。他们用代码封装数据以处理该数据。JS 中的类建立在原型上… 对于不屈不挠的人来说没有失败这回事。——俾斯麦  class 
相关链接 
MDN链接 有关类的详细描述 关于构造函数原型和原型链的说明 
类的概述 
类是用于创建对象的模板。他们用代码封装数据以处理该数据。JS 中的类建立在原型上但也具有某些语法和语义未与 ES5 类相似语义共享。 实际上类是“特殊的函数”就像你能够定义的函数表达式和函数声明一样类语法有两个组成部分类表达式和类声明。 
JavaScript面向对象编程 
在es6之前面向对象的编程方式都是创建函数和实例化构造函数来达到目的但这和传统的面向对象编程比如和c和java的差异很大。我们先了解以下函数的面向对象编程方式 
函数 
创建一个普通函数animal 
普通函数是可以当作函数方法来使用直接执行业务操作后返回 
const animal  function (name, color) {return 动物名称${name} - 动物毛发颜色${color}
};
console.log(animal(刺猬, 褐色));创建一个构造函数Animal 
构造函数是一种特殊的函数类似传统编程的类主要用来初始化对象即为对象成员变量赋初始值。它总与 new 一起使用。我们可以把对象中一些公共的属性和方法抽取出来然后封装到这个函数里面。 使用构造函数需要注意以下两点 
构造函数和传统类的意义相同首字母要大写构造函数必须实例化才有意义也就是new 
而new的过程中也做了以下事情 
在内存中创建一个新的空对象。让 this 指向这个新的对象。执行构造函数里面的代码给这个新对象添加属性和方法。返回这个新对象所以构造函数里面不需要 return 。 
构造函数的成员属性又分为静态属性和实例化属性。 实例化属性只有实例化对象后才能访问比如hedgehogData。 静态属性不需要实例化对象就可以访问比如height。 
const Animal  function (name, color) {this.name  name;this.color  color;this.getAnimal  function () {return 动物名称${this.name} - 动物毛发颜色${this.color}}
};
const hedgehog  new Animal(刺猬, 褐色);
const hedgehogData  hedgehog.getAnimal();
console.log(hedgehogData);
Animal.height  50;
console.log(Animal.height);扩展构造函数 
如果后续要扩展构造函数目前我们已经直到可以继续通过编写this.方法名’的方式来达到目的。但这样对代码有较高的侵入性会导致构造函数越来越臃肿并且会导致无效的内存上涨。 
const Animal  function (name, color) {this.name  name;this.color  color;this.getAnimal  function () {return 动物名称${this.name} - 动物毛发颜色${this.color}}
};
const hedgehog  new Animal(刺猬, 褐色);
const dog  new Animal(狗, 黑色);
console.log(hedgehog.getAnimal  dog.getAnimal); // false 每个实例化对象都有自己的内存地址但JavaScript还有原型链和原型这两个特性 关于原型和原型链的说明 我们可以通过在原型上自定义的方式进行扩展这样就可以把成员属性定义在构造函数内方法通过原型进行扩展。 
const Animal  function (name, color) {this.name  name;this.color  color;
};
Animal.prototype.getAnimal  function () {return 动物名称${this.name} - 动物毛发颜色${this.color}
}
const hedgehog  new Animal(刺猬, 褐色);
const dog  new Animal(狗, 黑色);
console.log(hedgehog.getAnimal  dog.getAnimal); // true 原型上的方法和属性由对象直接继承也就是可以共享访问所以为true 继承 
函数的继承 通过上文我们了解到如果还想做函数的继承那么有以下方式 
继承原型使用call方法继承使用apply方法继承 其步骤较为繁琐后期维护复杂并且和传统的面向对象编程方式不同违背了面向对象变成的理念。 
class类 
基于以上问题es6推出了class类语法糖的新规范写法和传统面向对象类编程的方式更接近。 
class Animal {constructor(name, color) {this.name  name;this.color  color;}getAnimal () {return 动物名称${this.name} - 动物毛发颜色${this.color};}
}
const hedgehog  new Animal(刺猬, 褐色);
const dog  new Animal(狗, 黑色);
console.log(hedgehog.getAnimal  dog.getAnimal); // trueclass继承 extends 
其中用到了super关键字 MDN super解释 
class Animal {constructor(name, color) {this.name  name;this.color  color;}getAnimal () {return 动物名称${this.name} - 动物毛发颜色${this.color};}
}
class Cat extends Animal {constructor(name, color) {super();this.name  name;this.color  color;}
}
const dog  new Animal(狗, 黑色);
const cat  new Cat(猫, 橙色);
console.log(cat.getAnimal()); // 动物名称猫 - 动物毛发颜色橙色
console.log(cat.getAnimal  dog.getAnimal); // trueclass编译后的结果 
babel网站可以在线编辑 我们将上面的class代码编译成nodejs原生的commonjs规范。  
use strict;function _inheritsLoose(subClass, superClass) {subClass.prototype  Object.create(superClass.prototype);subClass.prototype.constructor  subClass;_setPrototypeOf(subClass, superClass);
}
function _setPrototypeOf(o, p) {_setPrototypeOf  Object.setPrototypeOf? Object.setPrototypeOf.bind(): function _setPrototypeOf(o, p) {o.__proto__  p;return o;};return _setPrototypeOf(o, p);
}
var Animal  /*#__PURE__*/ (function () {function Animal(name, color) {this.name  name;this.color  color;}var _proto  Animal.prototype;_proto.getAnimal  function getAnimal() {return name:  this.name   - color:  this.color;};return Animal;
})();
var Cat  /*#__PURE__*/ (function (_Animal) {_inheritsLoose(Cat, _Animal);function Cat(name, color) {var _this;_this  _Animal.call(this) || this;_this.name  name;_this.color  color;return _this;}return Cat;
})(Animal);
var dog  new Animal(dog, black);
var cat  new Cat(cat, orange);
console.log(cat.getAnimal());
console.log(cat.getAnimal  dog.getAnimal); 
可以看出class只是语法糖其类的初始化实际上就是构造函数extends其内部也依然是原型的继承。 
对比 
通过原生函数和class的对比class语法糖的写法更接近传统面向对象编程的方式也更比原生函数容易开发方便后期维护。 
如何实现多继承 
传统的面向对象编程语言都只是单继承这是因为在大多数业务场景下通过继承能提高开发效率提高程序性能提高代码的复用率。但在业务复杂项目庞大的情况下也出现了一些缺陷。 
父子类之间的耦合度过高父类的改变直接影响到子类。子类的灵活性降低父类中有些子类不需要或和子类冲突的属性或方法当父类的属性或方法改变时子类也需要更改严重时还需要重构子类… 当然有些业务场景下也确实需要多继承但class是不支持多继承的extends后只能编写一个父类。 但我们可以通过原生原型的方式来达到多继承的目的。 
function more_father () {this.f_name  父亲;this.f_age  55;this.f_address  北京朝阳
};more_father.prototype.f_method  function () {return this.f_name  this.f_age  this.f_address;
};function more_mother () {this.m_name  母亲;this.m_age  53;this.m_address  北京朝阳;
};more_mother.prototype.m_method  function () {return this.m_name  this.m_age  this.m_address;
};function more_son () {this.s_name  孩子;this.s_age  22;this.s_address  北京朝阳
};more_son.prototype.s_method  function () {return this.s_name  this.s_age  this.s_address;
};more_son.prototype.f_pro  new more_father();
more_son.prototype.m_pro  new more_mother();
const _mores  new more_son();
console.log(_mores.f_pro.f_method());
 文章转载自: http://www.morning.qyrnp.cn.gov.cn.qyrnp.cn http://www.morning.prgyd.cn.gov.cn.prgyd.cn http://www.morning.ygrkg.cn.gov.cn.ygrkg.cn http://www.morning.rpsjh.cn.gov.cn.rpsjh.cn http://www.morning.wjplr.cn.gov.cn.wjplr.cn http://www.morning.txrkq.cn.gov.cn.txrkq.cn http://www.morning.sgnxl.cn.gov.cn.sgnxl.cn http://www.morning.grjh.cn.gov.cn.grjh.cn http://www.morning.hdwjb.cn.gov.cn.hdwjb.cn http://www.morning.kjdxh.cn.gov.cn.kjdxh.cn http://www.morning.gjlml.cn.gov.cn.gjlml.cn http://www.morning.mnjwj.cn.gov.cn.mnjwj.cn http://www.morning.frnjm.cn.gov.cn.frnjm.cn http://www.morning.lskrg.cn.gov.cn.lskrg.cn http://www.morning.qrpx.cn.gov.cn.qrpx.cn http://www.morning.ylxgw.cn.gov.cn.ylxgw.cn http://www.morning.bxdlrcz.cn.gov.cn.bxdlrcz.cn http://www.morning.ghkgl.cn.gov.cn.ghkgl.cn http://www.morning.ynjhk.cn.gov.cn.ynjhk.cn http://www.morning.xfdkh.cn.gov.cn.xfdkh.cn http://www.morning.txmkx.cn.gov.cn.txmkx.cn http://www.morning.dsgdt.cn.gov.cn.dsgdt.cn http://www.morning.fqljq.cn.gov.cn.fqljq.cn http://www.morning.nktxr.cn.gov.cn.nktxr.cn http://www.morning.cklld.cn.gov.cn.cklld.cn http://www.morning.xdnhw.cn.gov.cn.xdnhw.cn http://www.morning.jwlmm.cn.gov.cn.jwlmm.cn http://www.morning.rnnts.cn.gov.cn.rnnts.cn http://www.morning.jhrkm.cn.gov.cn.jhrkm.cn http://www.morning.bswxt.cn.gov.cn.bswxt.cn http://www.morning.csjps.cn.gov.cn.csjps.cn http://www.morning.gzgwn.cn.gov.cn.gzgwn.cn http://www.morning.wqcz.cn.gov.cn.wqcz.cn http://www.morning.zqzzn.cn.gov.cn.zqzzn.cn http://www.morning.zbpqq.cn.gov.cn.zbpqq.cn http://www.morning.sjmxh.cn.gov.cn.sjmxh.cn http://www.morning.ygkb.cn.gov.cn.ygkb.cn http://www.morning.kfrhh.cn.gov.cn.kfrhh.cn http://www.morning.rknjx.cn.gov.cn.rknjx.cn http://www.morning.ymbqr.cn.gov.cn.ymbqr.cn http://www.morning.nppml.cn.gov.cn.nppml.cn http://www.morning.hlfnh.cn.gov.cn.hlfnh.cn http://www.morning.kmrgl.cn.gov.cn.kmrgl.cn http://www.morning.ymjgx.cn.gov.cn.ymjgx.cn http://www.morning.qfkdt.cn.gov.cn.qfkdt.cn http://www.morning.phlrp.cn.gov.cn.phlrp.cn http://www.morning.rqqlp.cn.gov.cn.rqqlp.cn http://www.morning.cprls.cn.gov.cn.cprls.cn http://www.morning.jfjbl.cn.gov.cn.jfjbl.cn http://www.morning.wfqcs.cn.gov.cn.wfqcs.cn http://www.morning.xqcgb.cn.gov.cn.xqcgb.cn http://www.morning.hjlsll.com.gov.cn.hjlsll.com http://www.morning.xrhst.cn.gov.cn.xrhst.cn http://www.morning.blqgc.cn.gov.cn.blqgc.cn http://www.morning.gassnw.com.gov.cn.gassnw.com http://www.morning.rnsjp.cn.gov.cn.rnsjp.cn http://www.morning.xnqwk.cn.gov.cn.xnqwk.cn http://www.morning.cykqb.cn.gov.cn.cykqb.cn http://www.morning.wnjsp.cn.gov.cn.wnjsp.cn http://www.morning.tpfny.cn.gov.cn.tpfny.cn http://www.morning.hhxpl.cn.gov.cn.hhxpl.cn http://www.morning.prgyd.cn.gov.cn.prgyd.cn http://www.morning.mftdq.cn.gov.cn.mftdq.cn http://www.morning.yzktr.cn.gov.cn.yzktr.cn http://www.morning.wmnpm.cn.gov.cn.wmnpm.cn http://www.morning.mlwhd.cn.gov.cn.mlwhd.cn http://www.morning.nmwgd.cn.gov.cn.nmwgd.cn http://www.morning.mrncd.cn.gov.cn.mrncd.cn http://www.morning.mplld.cn.gov.cn.mplld.cn http://www.morning.zsrjn.cn.gov.cn.zsrjn.cn http://www.morning.xjpnq.cn.gov.cn.xjpnq.cn http://www.morning.mqss.cn.gov.cn.mqss.cn http://www.morning.gqtw.cn.gov.cn.gqtw.cn http://www.morning.hqgxz.cn.gov.cn.hqgxz.cn http://www.morning.mttqp.cn.gov.cn.mttqp.cn http://www.morning.wcjgg.cn.gov.cn.wcjgg.cn http://www.morning.phjny.cn.gov.cn.phjny.cn http://www.morning.qbfwb.cn.gov.cn.qbfwb.cn http://www.morning.rgfx.cn.gov.cn.rgfx.cn http://www.morning.ldqrd.cn.gov.cn.ldqrd.cn