我现在有这段代码,我想知道是否有一种方法可以内联地进行:
export const defaultArrayType = {
'string': defaultString,
'boolean': defaultBoolean,
'number': defaultNumber,
'object': defaultObject
};
Object.defineProperty(defaultArrayType, 'undefined', {
get() {
throw new Error('Array cannot contain an <undefined> element.');
}
});
Object.defineProperty(defaultArrayType, 'symbol', {
get() {
throw new Error('Array cannot contain a <Symbol> element.');
}
});
我想知道是否有一种内联的方法,像这样:
export const defaultArrayType = {
'string': defaultString,
'boolean': defaultBoolean,
'number': defaultNumber,
'object': defaultObject,
'undefined': definePropertySomethingSomething(), // << ?
'symbol': definePropertySomethingSomething() // << ?
};
答案 0 :(得分:2)
这如何:
export const defaultArrayType = {
'string': defaultString,
'boolean': defaultBoolean,
'number': defaultNumber,
'object': defaultObject,
get 'undefined'() {throw new Error('Array cannot contain an <undefined> element.');},
get 'symbol'() {throw new Error('Array cannot contain a <Symbol> element.');}
};