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 判断对象


浙公网安备 33010602011771号