ES6部分源码重写 -3(ES6-ES5的方式来手写部分源码)
时间:2022-07-25
本文章向大家介绍ES6部分源码重写 -3(ES6-ES5的方式来手写部分源码),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
ES6中的多出的功能
主要完成一下几个功能
- 对象必须要new的方式才能出来
- 创建对象
- 这里创建对象的方法采用立即执行函数,封闭作用域,避免将私有变量暴露
- 因为使用了立即执行函数,执行后就销毁,所以需要将构造函数返回给Plane这个变量
var Plane = (function(){
function Plane(name){
this.name = name || "一般飞机";
this.blood = 100;
}
return Plane;
})()
- 定义原型上的方法和静态方法
- 实现ES6中的构造函数必须通过new的方式来创建
- 这里判断是不是new的主要方式是通过是不是new出来的
- 如果是new出来的this指向的是Plane,如果不是new出来的,this指向的是window
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的方式创建
- 添加原型上的方法和静态方法
-
- 先创建一个子类的对象,用做继承使用
- 将Plane构造函数传入立即执行函数中,避免每次都去全局中取,节省开发效率
- 继承父类的私有属性Plane.apply(this, Plane);
- 继承原型上的方法
//封装原型上的方法和构造函数上的方法
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)
- 子类继承父类
- 此处子类继承父类调用的是ES6的方式,如果老版本的浏览器不支持,需要做兼容性处理, 使用的是AttackPlane.prototype.proto = Plane.prototype;此处就没有去做兼容性处理了
//继承的方法
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");
修改了子类的的构造函数的方法以后,可以成功的解决刚才的问题 效果如下图
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 有赞Flutter插件开发与发布
- 国标GB28181协议客户端EasyGBS国标视频平台级联EasyNVR:EasyGBS如何实现调阅EasyNVR的视频通道?
- gitlab CI/CD 相关问题
- 微信jssdk分享接口
- 有赞 Flutter 混编方案
- 最近开发问题
- echarts相关问题总结
- 视频上云EasyNTS组网硬件设备登录后自动下线并清除设备信息是什么原因?
- 震惊! 再也不怕蹭网被发现了!
- 国标GB28181协议客户端EasyGBS国标视频平台级联EasyNVR:EasyNVR到EasyGBS上是如何注册及注销的?
- git全局忽略文件配置
- 2018-10-16近期vue开发总结
- 有赞埋点实践
- 树莓派配置LEDE(openwrt衍生版)拨号
- Android 沉浸式解析和轮子使用