博客
关于我
对于类的构造函数的this.XXX=XXX写起来麻烦,使用proxy来偷懒
阅读量:669 次
发布时间:2019-03-15

本文共 936 字,大约阅读时间需要 3 分钟。

//创建用户类class User {    //对于写个给太麻烦了    // constructor(firstName,lastName,age) {    //     this.firstName=firstName;    //     this.lastName=lastName;    //     this.age=age;    // }}/** * 直接对构造函数进行代理 * @param Class 对象 * @param propNames 对应构造函数的参数 * @returns {any} * @constructor */function ConstructorProxy(Class, ...propNames) {    return new Proxy(Class, {        construct(target, argumentsList) {            const obj = Reflect.construct(target, argumentsList)            propNames.forEach((name, i) => {                obj[name] = argumentsList[i];            })            return obj;        }    })}const UserProxy = ConstructorProxy(User, "firstName", "lastName", "age")console.log(UserProxy)const obj = new UserProxy("w", "q", 18);console.log(obj)//创建Monster类class Monster {}const MonsterProxy = ConstructorProxy(Monster, "attack", "defence", "hp", "rate", "name")const m = new MonsterProxy(10, 20, 100, 30, "怪物")console.log(m);

 

 

 

转载地址:http://rsamz.baihongyu.com/

你可能感兴趣的文章
Mysql定时备份脚本
查看>>
mysql实战01|基础架构:一条SQL查询语句是如何执行的?
查看>>
Mysql实战之数据备份
查看>>
MySQL实战教程:从小白到大神的进阶之路!
查看>>
mysql实现成绩排名
查看>>
Mysql客户端中文乱码问题解决
查看>>
mysql客户端工具使用
查看>>
MySQL密码忘记,怎么办?
查看>>
mysql对同一张表进行查询和赋值更新
查看>>
mysql导入数据库出现:Incorrect string value: '\xE7\x82\xB9\xE9\x92\x9F' for column 'chinese' at row 1...
查看>>
mysql导入(ibd文件)
查看>>
Mysql工作笔记006---Mysql服务器磁盘爆满了_java.sql.SQLException: Error writing file ‘tmp/MYfXO41p‘
查看>>
MySQL工具1:mysqladmin
查看>>
mysql常用命令
查看>>
MySQL常用命令
查看>>
mysql常用命令
查看>>
MySQL常用指令集
查看>>
mysql常用操作
查看>>
MySQL常用日期格式转换函数、字符串函数、聚合函数详
查看>>
MySQL常见函数
查看>>