js中arguments对象和this对象

js中arguments对象和this属性

如果不注重复习,花时间准备的材料毫无意义

arguments对象和this对象都是对象

直接来代码

 1 <!DOCTYPE html>
 2 <html lang="zh-cn">
 3 <head>
 4     <meta charset="utf-8">
 5     <title>1-3 课堂演示</title>
 6     <style type="text/css">
 7 
 8     </style>
 9 </head>
10 <body>    
11     <script>
12         //arguments.length检测函数的参数个数
13         function sum() {
14             //alert(arguments.length);
15             var result=0;
16             for(var i=0;i<arguments.length;i++){
17                   result+=arguments[i]
18             }
19             return result
20         }
21         //sum();
22         //sum(12);
23         //sum(12,3,5);
24         //alert(sum(12,3,5,10,5,3))
25     </script>
26     <script>
27         //在函数外部使用this,this就指的是window对象
28         //alert(this)
29 
30         //全局变量可以看做window对象的属性
31         var x=1;
32         alert(window.x)
33         alert(this.x)
34         
35     </script>
36 </body>
37 </html>

函数的this属性

js中的属性都是用.(点),并且变量和函数不用写变量类型,和php一样,但是多了var,这点和php不一样。

js和php函数的话都带function关键字,这和强类型语言java和c等不一样。

 1 <!DOCTYPE html>
 2 <html lang="zh-cn">
 3 <head>
 4     <meta charset="utf-8">
 5     <title>1-4 课堂演示</title>
 6     <style type="text/css">
 7 
 8     </style>
 9 </head>
10 <body>
11 <div id="div1" style=" 100px;height: 100px;background: red">11111111</div>    
12     <script>
13         //在函数外部使用this,this就指的是window对象
14         //alert(this)
15 
16         //全局变量可以看做window对象的属性
17         var x=1;
18         //alert(window.x)
19         //alert(this.x)
20 
21         //函数内部调用
22         function test(){
23             var x=0;
24             alert(x) //这里的x为0
25             alert(this.x); //这里的x为1
26             alert(this)
27         }
28         //test()
29         //用new来调用,那么绑定的将是新创建的对象
30         function test2(){ 
31         this.x = 100; 
32       } 
33         var obj = new test2();
34         //alert(x); //这里的x为1
35         //alert(obj.x);//这里的x为100
36 
37         //作为某个对象的方法调用
38         function test3(){ 
39         alert(this.x); 
40       } 
41         var objo={}; 
42         objo.x = 1000; 
43         objo.m = test3; 
44         //alert(x);
45         //objo.m(); //1000
46 
47         //事件监听函数中的this
48         var div1 = document.getElementById('div1');
49         div1.onclick = function(){
50             alert( this.innerHTML); //this指向的是div元素,输出11111111
51         };
52         
53     </script>
54 </body>
55 </html>
原文地址:https://www.cnblogs.com/Renyi-Fan/p/8896584.html