javascript是弱类型语言,一个函数参数可以接收不同类型的变量作为它的该参数。
如:noticeEstateSales的week参数可以是空,数字类型(4),字符串(‘登记’, ‘4’)类型。
这在强类型语言(如:Object c,java)中是无法理解的。
当然弱类型语言好处也有,就是因为它不太讲究,健壮性很高,不用crash。缺点是:当出现问题时,不容易定位。如:一个地方调用是正常的,另一个地方调用就有可能是异常的。
下面是一个例子函数:
erp_notice_saler.noticeEstateSales = async function (week, estate_id) {
logger.debug('noticeEstateSales timer: start');
let wordsMap = {
'1': '快',
'2': '上',
'3': '上',
'4': '快来',
'登记': '您',
};
let words = wordsMap[week];
}
传递不同的参数调用:
const date = new Date();
const week = date.getDay();
erp_notice_saler.noticeEstateSales(week);
erp_notice_saler.noticeEstateSales(4);
erp_notice_saler.noticeEstateSales(5);
erp_notice_saler.noticeEstateSales('4');
打印日志:
[2024-05-13T12:00:25.151] [DEBUG] app - noticeEstateSales timer: week: 1
[2024-05-13T12:00:25.151] [DEBUG] app - noticeEstateSales timer: wordsMap[week] 快来更新预计开盘信息,能同时获得积分和展位,先到先审先得!~ week: 1
[2024-05-13T12:00:25.151] [DEBUG] app - noticeEstateSales timer: start
[2024-05-13T12:00:25.151] [DEBUG] app - noticeEstateSales timer: week: 4
[2024-05-13T12:00:25.151] [DEBUG] app - noticeEstateSales timer: wordsMap[week] 快来 week: 4
[2024-05-13T12:00:25.151] [DEBUG] app - noticeEstateSales timer: start
[2024-05-13T12:00:25.151] [DEBUG] app - noticeEstateSales timer: week: 5
[2024-05-13T12:00:25.151] [DEBUG] app - noticeEstateSales timer: wordsMap[week] undefined week: 5
[2024-05-13T12:00:25.151] [DEBUG] app - noticeEstateSales timer: start
[2024-05-13T12:00:25.151] [DEBUG] app - noticeEstateSales timer: week: 4
[2024-05-13T12:00:25.151] [DEBUG] app - noticeEstateSales timer: wordsMap[week] 快来 week: 4