基础不牢,房塌屋倒。javascript如果没有把基础知识掌握牢固的话,可能在实践中遇见一些本来很简单的实现方式,却劳师动众的用各种组合封装函数来实现一个简单的功能。
function() {}()
(function() { const now = new Date(); const year = now.getFullYear(); const month = now.getMonth()+1; const day = now.getDate(); $("#nowDate").html(year+"年"+month+"月"+day+"日"); })();
等同于
//获取当天日期 $(function () { const now = new Date(); const year = now.getFullYear(); const month = now.getMonth() + 1; const day = now.getDate(); $("#nowDate").html(year + "年" + month + "月" + day + "日"); });
function showTime()
//获取当天日期 function showTime() { const now = new Date(); const year = now.getFullYear(); const month = now.getMonth() + 1; const day = now.getDate(); $("#nowDate").html(year + "年" + month + "月" + day + "日"); } showTime();
等同于
//获取当天日期 var showTime = function () { const now = new Date(); const year = now.getFullYear(); const month = now.getMonth() + 1; const day = now.getDate(); $("#nowDate").html(year + "年" + month + "月" + day + "日"); } showTime();
Done!