JS 方法的定义和调用 和Date日期对象

定义和调用

let person={
        name:"jie",
        birth:2000,
        //方法
        age: function () {
            let now=new Date().getFullYear();
            return now-this.birth;
        }

    }

apply

可以控制this执行的对象

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    function getAge(){
        let now=new Date().getFullYear();
        return now-this.birth;
    }
    let person={
        name:"jie",
        birth:2000,
        //方法
        age: getAge
    };
    getAge().apply(person,[])//this指向person,参数为空
</script>
</body>
</html>

Date对象

可以通过typeof 判断对象

posted @ 2021-05-07 11:03  一个经常掉线的人  阅读(115)  评论(0)    收藏  举报