首页 > 编程语言 > 详细

javaScript对象

时间:2017-02-07 13:35:26      阅读:174      评论:0      收藏:0      [点我收藏+]
 1 <!DOCTYPE html>
 2 <html>
 3 
 4     <head>
 5         <meta charset="utf-8" />
 6         <title></title>
 7     </head>
 8 
 9     <body>
10 
11     </body>
12 
13     <script>
14         //        var user = {
15         //            name: ‘tom‘,
16         //            age: 20,
17         //            worke: ‘工人‘,
18         //            play: function() {
19         //                alert(‘打篮球‘);
20         //            },
21         //            habbit: [‘音乐‘, ‘相声‘, ‘游戏‘, ‘电影‘]
22         //        }
23         //        alert(user.play instanceof Function)
24         //        if(user.play instanceof Function) {
25         //            user.play();
26         //        }
27         //        for(var a in user) {
28         //            //alert(user[a] instanceof Function);
29         //            if(user[a] instanceof Function) {
30         //                user[a]();
31         //            } else if(user[a] instanceof Array) {
32         //                for(var i = 0; i < user[a].length; i++) {
33         //                    alert(user[a][i]);
34         //                }
35         //            } else {
36         //                alert(user[a]);
37         //            }
38         //        }
39         //        
40 
41         function Student(name, age) {
42             this.name = name;
43             this.age = age;
44             this.test = function() {
45                 alert(this.name);
46                 alert(this);
47             }
48         }
49         var stu = new Student(tom, 20);
50         Student.s = 2222; //类属性,只有类才能访问到,对象访问不到(类方法也是这样)
51         alert(stu.s); //结果 undefined
52         alert(Student.s); //结果 2222
53         var name = window全局属性;  //等同于  window.name=‘window全局属性‘
54        //this代表当前对象,但有时也要注意this的确切指向
55         alert(stu.name);
56         alert(stu.age);
57         stu.test();//this 指向stu
58         //等同于 var f=function(){alert(this.name);alert(this);} 此时的this不再是stu,而是全局对象window
59         var f = stu.test; 
60         f(); //等同于 window.f()  this 指向window
61     </script>
62 
63 </html>

 

javaScript对象

原文:http://www.cnblogs.com/java-le/p/6373509.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!