Javascript 使用函数设置对象的值
我试图用一个函数设置命名对象键的值Javascript 使用函数设置对象的值,javascript,google-apps-script,Javascript,Google Apps Script,我试图用一个函数设置命名对象键的值SORT,如下所示: statementPdfs.push ({ NAME: currFile.getName(), ID: currFile.getId(), BLOB: currFile.getBlob(), MIME_TYPE: currFile.getBlob().getContentType(), URL: currFile.getUrl(), SORT: () =&g
SORT
,如下所示:
statementPdfs.push ({
NAME: currFile.getName(),
ID: currFile.getId(),
BLOB: currFile.getBlob(),
MIME_TYPE: currFile.getBlob().getContentType(),
URL: currFile.getUrl(),
SORT: () => {
let dates = currFile.getName().match(/(\w+)\s(\d+)/);
return (dates[2]+
(dateNums[dates[1].toUpperCase()] < 10
? "0"+dateNums[dates[1].toUpperCase()]
: dateNums[dates[1].toUpperCase()]
)
);
}
});
statementPdfs.push({
名称:currFile.getName(),
ID:currFile.getId(),
BLOB:currFile.getBlob(),
MIME_类型:currFile.getBlob().getContentType(),
URL:currFile.getUrl(),
排序:()=>{
让dates=currFile.getName().match(/(\w+)\s(\d+/);
申报表(日期[2]+
(dateNums[dates[1].toUpperCase()]<10
?“0”+dateNums[日期[1]。toUpperCase()]
:dateNums[dates[1].toUpperCase()]
)
);
}
});
…但是
SORT
属性的返回值是函数,而不是返回值。当然,一定有一个简单的答案,但我找不到…您需要一个被称为立即调用的函数表达式(IIFE):将函数包装在(…)()
中:
SORT: (() => {
let dates = currFile.getName().match(/(\w+)\s(\d+)/);
return (dates[2]+
(dateNums[dates[1].toUpperCase()] < 10
? "0"+dateNums[dates[1].toUpperCase()]
: dateNums[dates[1].toUpperCase()]
)
);
})()
SORT: (() => {
let dates = currFile.getName().match(/(\w+)\s(\d+)/);
return dates[2]+dateNums[dates[1].toUpperCase()].padStart(2, "0");
})()