1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <title>Title</title>
6
7
8 <script>
9 //定义方法
10 //方法就是把函数放在对象里,对象只有两个东西:属性和方法
11 // let wty = {
12 // name: 'wty',
13 // birth: 1995,
14 // //方法
15 // age: function(){
16 // //今年-出生年
17 // let now = new Date().getFullYear();
18 // return now - this.birth;
19 // }
20 // };
21 //wty.age() 调用方法不要忘记括号
22 //wty.name 属性
23
24
25
26 //拆分方法
27 let wty1 = {
28 name: 'wty',
29 birth: 1995,
30 //方法
31 age: getAge
32 }
33 function getAge(){
34 //今年-出生年
35 let now = new Date().getFullYear();
36 return now - this.birth;
37 }
38 //this代表什么,this是无法指向的,this默认指向调用的对象
39
40 //apply 任何函数都有apply属性
41 // 在js中可以控制this指向
42 getAge().apply(wty1,[]);//this指向了wty1对象 参数为空
43 </script>
44 </head>
45 <body>
46
47 </body>
48 </html>