JavaScript高级编程——引用类型、Array数组使用、栈方法

JavaScript高级编程——引用类型、Array数组使用、栈方法

  

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>引用类型</title>
</head>
<body>
    <script>

        /*
          创建Object类型实例的方式有两种
          1.使用New操作后跟Object构造函数
          2.对象字面量表示法
        */

        //使用New操作后跟Object构造函数
        var person = new Object();
        person.name = "Nicholas";
        person.age = 18;

        //与new Object()相同,而且能给人封装数据的感觉
        var personlog = {};
        personlog.name = "Nicholas";
        personlog.age = 18;

        //对象字面量表示法,在使用对象字面量语法时,属性名也可以使用字符串
        var persontwo =
            {
                name: "Nichola",
                age: 29,
                5: true
            }
        console.log(person);
        console.log(persontwo);

        function dispalyInfo(args) {
            var output = "";
            if (typeof args.name == "string") {
                output += "Name:" + args.name + "\n";
            }
            if (typeof args.age == "number") {
                output += "Age:" + args.age + "\n";
            }
            alert(output);
        }

        dispalyInfo({ name: "Nichholas", age: 29 });
        dispalyInfo({ name: "Greg" });


        /*
            Array类型,创建数组的基本方式有两种

            第一种使用Array构造函数,可以设置length值为20的数组,也可以向Array构造函数传递数组中包含的值
            var colors = new Array(20);
            var colors = new Array("red","blue","green");
            在使用array构造函数时,也可以省略new操作符
            var colors = Array(20);
            var colors = Array("red");

            第两种创建数组的第二种基本方式是使用数组字面量表示法,数组字面量由一对包含数组项的方括号表示,
            多个数组项之间以逗号隔开, var names = [];创建一个空数组

        */
        var colors = new Array();
        var colorssplit = ["red", "blue", "green"];
        var names = [];

        alert("colorssplit的数组长度为:" + colorssplit.length);  //3
        alert("空数组的数组长度为:" + names.length);   //0

        console.log("toString:" + colorssplit.toString());
        console.log("valueOf:" + colorssplit.valueOf());
        console.log("colorssplit:" + colorssplit);
        alert("使用toString返回数组的值" + colorssplit.toString());
        alert("使用valueOf返回数组的值" + colorssplit.valueOf());
        alert("直接返回数组的值" + colorssplit);

        /*
          toString和toLocaleString
        */
        var person1 =
            {
                toLocaleString: function () {
                    return "Nikolaos";
                },
                toString: function () {
                    return "Nicholas";
                }
            };

        var person2 =
           {
               toLocaleString: function () {
                   return "Grigorios";
               },
               toString: function () {
                   return "Greg";
               }
           };

        var people = [person1, person2];
        alert("people:" + people);
        alert("people.toString:" + people.toString());
        alert("people.toLocaleString:" + people.toLocaleString());

        console.log("people.toString:" + people.toString());
        console.log("people.toLocaleString:" + people.toLocaleString());

        //使用join方法,可以使用不同的分隔符来构建这个字符串
        var colorjoin = ["red", "blue", "green"];
        alert(colorjoin.join(","));
        alert(colorjoin.join("||"));
        console.log("colorjoin:使用,逗号" + colorjoin.join(","));
        console.log("colorjoin:使用||分号" + colorjoin.join("||"));

        /*栈方法 push方法和pop
            push()方法可以接收任意数量的参数,把它们逐个添加到数组的末尾,并返回修改后数组的长度
            pop()方法则从数组末尾移除最后一项,减少length值,然后返回移除的值
        */
        alert("进入栈方法 push方法和pop")
        var colorspush = Array();  //创建一个数组
        var count = colorspush.push("red", "green");//推入两项
        alert("推入(red、green)" + count);

        count = colorspush.push("black"); //推入另一项
        alert("推入(red、green、black)" + count);

        var item = colorspush.pop();  //取得最后一项
        alert("使用pop得到是black:"+item);
        alert("使用pop方法数组移除一位,数组长度为:"+colorspush.length);  //2
        /*
            队列方法,栈数据结构的访问规则是Lifo(后进先出),队列在列表的末端添加项,从列表的前端移除项
            由于push()是向数组末端添加项的方法,因此要模拟队列只需要一个从数组前端取得项的方法,实现这
            一操作的数组方法就是Shift(),它能移除数组中的第一个项并返回该项,同时数组长度减1,
            结合使用shift和push就可以像使用队列一样使用数组
            ECMAScript还为数组提供了一个unshift()方法,与shift的用途相反
            它能在数组前端添加任意个项并返回数组的长度,可以从相反的方向来模拟队列
        */
        var colorspop = Array();  //创建一个数组
        var countpop = colorspop.push("red", "blue"); //推入两项
        alert("推入(red、blue)" + countpop);
        countpop = colorspop.push("black");
        alert(countpop);

        var itempop = colorspop.shift();  //取得第一项
        alert("使用shift取得第一项:"+itempop);  //pop取得是red
        alert("使用shift数组长度减一:" + colorspop.length); //返回的length值是2,shift取得第一项,同时数组的长度减1


        var colorUnShift = Array();  //创建一个数组
        var countUnShift = colorUnShift.unshift("red", "green");  //推入两项
        alert("使用unshift推入:" + countUnShift); //2
        countUnShift = colorUnShift.unshift("black");
        alert("使用unshift再次推入:" + countUnShift); //3
        var itemUnShift = colorUnShift.pop();
        alert("获取最后一项:" + itemUnShift); //green
        alert(colorUnShift.length); //2

    </script>
</body>
</html>

  

猜你喜欢

转载自www.cnblogs.com/chaonuanxi/p/10555906.html