js创建一个struct
function defStruct(fun) {
let ins = {
[fun.name] : function() {
let obj = fun();
Object.defineProperties(obj, {
constructor: {
value: fun,
enumerable: false,
writable: true,
configurable: true,
},
})
if (obj.init) {
obj.init()
}
return obj
}
}
return ins[fun.name]
}
let Test = defStruct(
function Test() {
prop1 = 1;
function fun() {
this.prop1 = 8;
}
return {
prop1,
fun
}
}
)
let s = Test();
function defStruct(fun) {
let ins = {
[fun.name] : function() {
let obj = fun();
Object.defineProperties(obj, {
constructor: {
value: fun,
enumerable: false,
writable: true,
configurable: true,
},
})
if (obj.init) {
obj.init()
}
return obj
}
}
return ins[fun.name]
}
let Test = defStruct(
function Test() {
prop1 = 1;
function fun() {
this.prop1 = 8;
}
return {
prop1,
fun
}
}
)
let s = Test();
展开
评论
点赞