创建一个 class,它使用 es6 class 语法将 Function 对象创建为实例
Create a class that creates Function objects as instance using es6 class syntax
是否可以创建一个 class 以其原型上的方法实例化函数?我正在尝试将代码从原型结构转换为使用 es6 class 语法。这是起点的人为设计和过度简化的示例
function createFun(init) {
function fun(newDats) {
this.data = newDats;
// create universe
}
function internalMethod() {
}
fun.data = init;
fun.aMethod = function () {
internalMethod();
}
assign(fun, AnExtendableClass.prototype);
return fun;
}
// and can be used as such
fun = createFun('first');
fun('second');
fun.aMethod();
fun.methodFromExtendableClass('third')
这就是我尝试过的
class Fun extend AnExtendableClass {
constructor(init) {
super();
this.data = init;
function fun(newDats) {
this.data = newDatas;
//create universe
}
assign(fun, this);
return fun;
}
aMethod() {
}
}
不幸的是,这在 return 中没有任何方法时不起作用。
Is it possible to create a class that instantiates functions with methods on it's prototype?
是的,使用 ES6 可以 subclass Function
- 但是,这不是很好,因为构造函数需要代码字符串:
class Fun {
constructor() {
super("newDats", `
this.data = newDats;
// create universe
`)
}
data() { }
aMethod() { }
}
let fun = new Fun;
fun(…);
fun.aMethod(…);
I am trying to convert code from prototype structure to using the es6 class syntax. Here is a contrived and over simplified example of the starting point
不要为此使用 class
语法。新语法非常有限,只能用于标准 class 声明。如果你做了任何奇怪的事情 - 从构造函数返回函数,从其他 classes 复制方法,分配静态属性和使用内部方法在这方面绝对是奇怪的 - 然后使用 "old",明确的方式.新的 Reflect.setPrototypeOf
在这里给了你更多的自由。
是否可以创建一个 class 以其原型上的方法实例化函数?我正在尝试将代码从原型结构转换为使用 es6 class 语法。这是起点的人为设计和过度简化的示例
function createFun(init) {
function fun(newDats) {
this.data = newDats;
// create universe
}
function internalMethod() {
}
fun.data = init;
fun.aMethod = function () {
internalMethod();
}
assign(fun, AnExtendableClass.prototype);
return fun;
}
// and can be used as such
fun = createFun('first');
fun('second');
fun.aMethod();
fun.methodFromExtendableClass('third')
这就是我尝试过的
class Fun extend AnExtendableClass {
constructor(init) {
super();
this.data = init;
function fun(newDats) {
this.data = newDatas;
//create universe
}
assign(fun, this);
return fun;
}
aMethod() {
}
}
不幸的是,这在 return 中没有任何方法时不起作用。
Is it possible to create a class that instantiates functions with methods on it's prototype?
是的,使用 ES6 可以 subclass Function
- 但是,这不是很好,因为构造函数需要代码字符串:
class Fun {
constructor() {
super("newDats", `
this.data = newDats;
// create universe
`)
}
data() { }
aMethod() { }
}
let fun = new Fun;
fun(…);
fun.aMethod(…);
I am trying to convert code from prototype structure to using the es6 class syntax. Here is a contrived and over simplified example of the starting point
不要为此使用 class
语法。新语法非常有限,只能用于标准 class 声明。如果你做了任何奇怪的事情 - 从构造函数返回函数,从其他 classes 复制方法,分配静态属性和使用内部方法在这方面绝对是奇怪的 - 然后使用 "old",明确的方式.新的 Reflect.setPrototypeOf
在这里给了你更多的自由。