您当前的位置: 首页 >  Java

漏刻有时

暂无认证

  • 2浏览

    0关注

    717博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

javascript函数:function() {}()的实战案例理解

漏刻有时 发布时间:2020-10-23 12:04:54 ,浏览量:2

基础不牢,房塌屋倒。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!

关注
打赏
1661217259
查看更多评论
立即登录/注册

微信扫码登录

0.0390s