利用 es6 new.target 来对模拟抽象类
起源
最近在使用 Symbol 来做为唯一值,发现 Symbol 无法进行 new 操作,只能当作函数使用,只要进行了new 就会发生类型错误
new Symbol() // error Uncaught TypeError: Symbol is not a constructor at new Symbol (<anonymous>) at <anonymous>:1:1
在不考虑底层实现的情况下,在代码层面是否能够实现一个函数只可以进行调用而不可以进行 new 操作呢?思考之后如下写出:
function disConstructor() { if (this !== window) { throw new TypeError(' disConstructor is not a constructor') } console.log('gogo go') } // 测试结果如下 disConstructor() // gogo go new disConstructor() // error Uncaught TypeError: disConstructor is not a constructor at new disConstructor (<anonymous>:3:15) at <anonymous>:1:1
如果使用 nodejs,window 可以切换为 global, 代码运行结果不变,因为对于个人而言没有适用场景。于是就没有继续研究下去,可是最近在从新翻阅 es6 时候发现 new.target这个属性。
new.target 属性
介绍(引用 mdn 文档)
new.target属性允许你检测函数或构造方法是否是通过new运算符被调用的。
在通过new运算符被初始化的函数或构造方法中,new.target返回一个指向构造方法或函数的引用。在普通的函数调用中,new.target 的值是undefined。
这样的话 我们的代码就可以这样改为
function disConstructor() { // 普通的函数调用中,new.target 的值是undefined。 if (new.target) { throw new TypeError(' disConstructor is not a constructor') } console.log('gogo go') }
得到与上述代码一样的答案。
深入
难道 es6 特地添加的功能仅仅只能用于检查一下我们的函数调用方式吗?
在查阅的过程各种发现了大多数都方案都是用 new.target 写出只能被继承的类。类似于实现java的抽象类。
class Animal { constructor(name, age) { if (new.target === Animal) { throw new Error('Animal class can`t instantiate'); } this.name = name this.age = age } // 其他代码 ... } class Dog extends Animal{ constructor(name, age, sex) { super(name, age) this.sex = sex } } new Animal() // error Uncaught Error: Animal class can`t instantiate at new Animal (<anonymous>:4:13) at <anonymous>:1:1 new Dog('mimi', 12, '公') // Dog {name: "mimi", age: 12, sex: "公"}
但是 java抽象类抽象方法需要重写,这个是没有方案的。于是在测试与使用的过程中,却意外发现了超类可以在构造期间访问派生类的原型,利用起来。
class Animal { constructor(name, age) { console.log(new.target.prototype) } // 其他代码 ... }
之前运行时调用需要重写的方法报错是这样写的。
class Animal { constructor(name, age) { this.name = name this.age = age } getName () { throw new Error('please overwrite getName method') } } class Dog extends Animal{ constructor(name, age, sex) { super(name, age) this.sex = sex } } const pite = new Dog('pite', 2, '公') a.getName() // error Uncaught Error: please overwrite getName method at Dog.getName (<anonymous>:8:11) at <anonymous>:1:3
然而此时利用 new.target ,我就可以利用 构造期间 对子类进行操作报错。
class Animal { constructor(name, age) { // 如果 target 不是 基类 且 没有 getName 报错 if (new.target !== Animal && !new.target.prototype.hasOwnProperty('getName')) { throw new Error('please overwrite getName method') } this.name = name this.age = age } } class Dog extends Animal{ constructor(name, age, sex) { super(name, age) this.sex = sex } } const pite = new Dog('pite', 2, '公') // error Uncaught Error: please overwrite getName method at new Animal (<anonymous>:5:13) at new Dog (<anonymous>:14:5) at <anonymous>:1:1
此时可以把运行方法时候发生的错误提前到构造时期,虽然都是在运行期,但是该错误触发机制要早危害要大。反而对代码是一种保护。
当然了,利用超类可以在构造期间访问派生类的原型作用远远不是那么简单,必然是很强大的,可以结合业务场景谈一谈理解和作用。
其他方案
增加 编辑器插件
proxy
修饰器
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
【奇思妙想】如何给网关设计一款专属的权限控制【责任链设计模式】
写在前面:设计模式源于生活,而又高于生活! 什么是责任链模式 客户端发出一个请求,链上的对象都有机会来处理这一请求,而客户端不需要知道谁是具体的处理对象。这样就实现了请求者和接受者之间的解耦,并且在客户端可以实现动态的组合职责链。使编程更有灵活性。 关键点 1、有多个对象共同对一个任务进行处理。 2、这些对象使用链式存储结构,形成一个链,每个对象知道自己的下一个对象。 3、一个对象对任务进行处理,可以添加一些操作后将对象传递个下一个任务。也可以在此对象上结束任务的处理,并结束任务。 4、客户端负责组装链式结构,但是客户端不需要关心最终是谁来处理了任务。 责任链模式环境 1.抽象处理者(Handler)角色:定义出一个处理请求的接口 2.具体处理者(ConcreteHandler)角色:具体处理者接到请求后,可以选择将请求处理掉,或者将请求传给下家。 责任链模式优缺点 优点: 职责链模式的最主要功能就是:动态组合,请求者和接受者解耦。 请求者和接受者松散耦合:请求者不需要知道接受者,也不需要知道如何处理。每个职责对象只负责自己的职责范围,其他的交给后继者。各个组件间完全...
- 下一篇
centos7系统源码编译安装PHP7.3.5版本详细步骤
新增系统用户组和用户: [root@localhost ~]# groupadd webg [root@localhost ~]# useradd -g webg webu 下载 PHP7.3.5 [root@localhost ~]# mkdir devdir [root@localhost ~]# cd devdir/ [root@localhost devdir]# wget https://www.php.net/distributions/php-7.3.5.tar.gz -bash: wget: 未找到命令 [root@localhost devdir]# rpm -qa|grep wget [root@localhost devdir]# yum -y install wget [root@localhost devdir]# rpm -qa|grep wget wget-1.14-18.el7.x86_64 下载PHP7 解压 编译 wget -chttps://downloads.php.net/~cmb/php-7.3.4.tar.gz 编译参数...
相关文章
文章评论
共有0条评论来说两句吧...