zoukankan      html  css  js  c++  java
  • JavaScript学习笔记(3)

    JavaScript的数据类型

      字符型、数字、布尔、数组、对象、Null、Undefined

      数组的定义:

        var cars=new Array();

        cars[0]="Audi";

        cras[1]="BMW";

        cars[3]="Benze";

      或者:

        var cars = new Array("Audi","BMW","Benze");

      或者:

        var cars =["Audi","Audi","Audi","Audi","Audi"];

    如:

    <!DOCTYPE html>
    <html>
    <body>

    <script>
    var i;
    //方式1
    var cars1 = new Array();
    cars1[0] = "Audi";
    cars1[1] = "BMW";
    cars1[2] = "Volvo";

    //方式2:
    var cars2 = new Array("Audi","BMW","Volvo","Audi","BMW","Volvo");

    //方式3
    var cars3=["Audi","Audi","Audi","Audi","Audi"];

    for (i=0;i<cars3.length;i++)
    {
    document.write(cars3[i] + "<br>");
    }
    </script>

    </body>
    </html>

    JavaScript对象

      对象由花括号分隔。在括号内部,对象的属性以名称和值对的形式 (name : value) 来定义。属性由逗号分隔:
        var person={firstname:"Bill", lastname:"Gates", id:5566};
      上面例子中的对象 (person) 有三个属性:firstname、lastname 以及 id。
      空格和折行无关紧要。声明可横跨多行:
        var person={
          firstname : "Bill",
          lastname : "Gates",
          id : 5566
         };
      对象属性有两种寻址方式:

        name=person.lastname;

        name=person["lastname"];

    如:

    <!DOCTYPE html>
    <html>
    <body>

    <script>
      var person={
      firstname : "Bill",
      lastname : "Gates",
      id : 5566
    };


    document.write(person.firstname+ "&nbsp"+person.lastname +"&nbsp" +person.id+"<br />");
    document.write(person["firstname"]+"&nbsp"+person["lastname"]+"&nbsp"+person["id"] + "<br />");
    </script>

    </body>
    </html>

    Undefined 和 Null

      undefined这个值表示变量不会含有值

      Null可以用来清空变量

    如:

    <!DOCTYPE html>
    <html>
    <body>

    <script>
    var person;
    var car="Volvo";
    document.write(person + "<br />");
    document.write(car + "<br />"+ "<br />");

    var car=null
    var person="Steve Jobs";
    document.write(person + "<br />");
    document.write(car + "<br />");
    </script>

    </body>
    </html>

    输出结果:

    undefined
    Volvo

    Steve Jobs
    null

    声明变量类型

      可以使用new关键字声明变量的类型

    如:

    <!DOCTYPE html>
    <html>
    <body>

    <button onclick="myFunction()">点击这里</button>

    <p id="test1"></p>
    <p id="test2"></p>
    <p id="test3"></p>
    <p id="test4"></p>

    <script>
    function myFunction()
    {
    var numVal = new Number;
    numVal = 123.123;

    var strVal = new String;
    strVal = "THIS IS A STRING.";

    var bVal = new Boolean;
    bVal = true;

    var arrVal = new Array;
    arrVal[0]=100;
    arrVal[2]=200;
    arrVal[3]=300;
    arrVal[4]=400;
    arrVal[5]=500;

    document.getElementById("test1").innerHTML=numVal;
    document.getElementById("test2").innerHTML=strVal;
    document.getElementById("test3").innerHTML=bVal;
    document.getElementById("test4").innerHTML=arrVal[3];
    }
    </script>

    </body>
    </html>

    输出结果:

    <!DOCTYPE html>
    <html>
    <body>

    <button onclick="myFunction()">点击这里</button>

    <p id="test1"></p>
    <p id="test2"></p>
    <p id="test3"></p>
    <p id="test4"></p>

    <script>
    function myFunction()
    {
      var numVal = new Number;
      numVal = 123.123;

      var strVal = new String;
      strVal = "THIS IS A STRING.";

      var bVal = new Boolean;
      bVal = true;

      var arrVal = new Array;
      arrVal[0]=100;
      arrVal[2]=200;
      arrVal[3]=300;
      arrVal[4]=400;
      arrVal[5]=500;

      document.getElementById("test1").innerHTML=numVal;
      document.getElementById("test2").innerHTML=strVal;
      document.getElementById("test3").innerHTML=bVal;
      document.getElementById("test4").innerHTML=arrVal[3];
    }
    </script>

    </body>
    </html>

    点击按钮后输出结果:

    123.123

    THIS IS A STRING.

    true

    300

  • 相关阅读:
    多项式乘法
    容斥计算多重组合
    D. Tokitsukaze, CSL and Stone Game
    优惠买商品(dp、greedy)
    数星星(单点更新,求前缀和)
    信息推送(单点更新,求前缀和)
    互相送礼物
    Codeforces Round #611 (Div. 3)E. New Year Parties
    多源bfs
    mysql事务和锁
  • 原文地址:https://www.cnblogs.com/chenliangwen/p/4422098.html
Copyright © 2011-2022 走看看