js 动态生成class的类名

class  A {}

就是这里类名A,我想生成不同的类,但生成的类名不同有什么办法吗?

阅读 5.1k
1 个回答

动态生成es6中的class名是不行的
但是动态生成属性名是可以的,所以可以通过下面方法实现es5中的类

var randomClassName = 'Xherhahfa';
window[randomClassName] = function (a, b) {
    this.a = a;
    this.b = b;
};
window[randomClassName].prototype.print = function () {
    console.log('a:', this.a);
    console.log('b:', this.b);
};
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题