zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    025、MySQL字符串大小写转化函数,文本转化大写,文本转化小写
    024、MySQL字符串替换函数,文本替换函数
    023、MySQL取文本长度取字符串长度
    022、MySQL字符串的拼接
    021、MySQL变量的使用,在MySQL中创建存储过程,并添加变量
    020、MySQL创建一个存储过程,显示存储过程,调用存储过程,删除存储过程
    019、MySQL取本季度开始时间和本季度结束时间
    018、MySQL取满足日期在两个日期之间的所有数据
    017、MySQL取第4本季度开始和结束日期
    016、MySQL取本年第一季度开始日期
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/8896584.html
Copyright © 2011-2022 走看看