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

    
        //===延长作用于链======
        // with()
        function buildUrl(){
            var qs="?debug=true";        
            with(location){
                var url = href +qs;
            }
            return url;
        }        
        var result1 = buildUrl();
        alert(result1);
        
        //====没有块级作用域=====
        if(true){
            var color = "blue";
        }
        alert(color); //->bule;在JavaScript中,if语句中的变量声明会将变量添加到当前的执行环境中。
        
        for(var i=0;i<10;i++){
            
        }
        alert(i); //->10; 对于JavaScript来说,由for语句创建的变量i即使在for循环执行结束后,也依旧会存在于循环体外部的执行环境中。
        
        //======1.变量声明=====
            //对比
    /*
            function add(num1,num2){
                var sum = num1+num2;
                return sum;
            }
            var result = add(10,20);
            alert(sum); //提示错误 sum 未定义
            
            function add(num1,num2){
                sum = num1+num2;
                return sum;
            }
            var result = add(10,20);
            alert(sum); //->30;sum是全局变量
    */
    //=======2.查询标识符=========
    //对比
        var color ="blue";
        function getColor(){
            return color;
        }
        //alert(getColor());//->"blue";查找过程:调用函数getColor()时会引用变量color。
                                    //(1)先搜索getColor()的变量对象,查找其中是否包含一个名为color的标识符。
                                    //(2)在没有找到的情况下,搜索继续到下一个变量对象(全局环境的变量对象),然后再那里找到了color
        function getColor1(){
            var color ="red";
            return color;
            //return window.color; //->"blue",此时访问的是全局环境的变量
        }
        alert(getColor1());    //->"red";(3)在找到的情况下,搜索就停止了。

    //===作用链域====== var color = "blue"; /* function changeColor(){ if(color=="blue"){ color ="red"; }else{ color="yellow"; } } changeColor(); alert("Color is now "+color);//输出 Color is now red */
    //====Object类型====== //一、创建Object实例的方式有两种。 //第一种是使用new操作符后跟Object构造函数 /* var person = new Object(); person.name = "Yoyo"; person.age = 24; alert(JSON.stringify(person)); */ //第二种是使用对象字面变量表示法 /* var person = { name:"YY", age:29 }; */ //alert(JSON.stringify(person)); //alert(person.name); //alert(person["name"]); //====对象字面量====== /* function displayInfo(args){ var output = ""; if (typeof args.name == "string") { output += "Name:" + args.name + " "; } if(typeof args.age=="number"){ output+="Age:"+args.age+" "; } alert(output); } displayInfo({ name:"YY", age:24 }); displayInfo({name:"Yoyo"}); */ //========Array========= var colors = ["red","blue","green"]; //alert(colors[3]); colors[2]="black"; colors[4]="brown"; //当大于数组长度时,会自动添加到数组,长度加1. //alert(colors); //if(colors instanceof Array){ //instanceof Array确定某个对象是不是数组 // alert("colors is Array."); //} //======1.arrayObject.join(separator)==== alert(colors.join("-")); //若某一项是null或者undefined,那么该值在join()、toLocaleString()、toString()和valueOf()方法的返回的结果中以空字符串表示。
    //===栈方法====
    var colors = new Array();
    var count = colors.push("red","green");
    //alert(count); //->2
    //alert(colors); //red,green
    
    count = colors.push("black");
    alert(colors.length); //->3
    alert(count); //->3
    
    var item = colors.pop();
    alert(item); //->black
    alert(colors.length); //->2
    //===shift()与 push()结合使用模拟队列====
    var colors = new Array();
    colors.push("blue","red");
    alert(colors); //blue,red
    var item = colors.shift();
    alert(item); //blue; blue出队
    alert(colors); //red; colors长度减1

      

    1 //====unshift()与pop()结合反向模拟队列=====
    2 var colors = new Array();
    3 var count = colors.unshift("blue","red");
    4 alert(count); //2
    5 alert(colors);//blue,red
    6 colors.unshift("hello"); //插入到队头
    7 alert(colors); //hello,blue,red
    8 colors.pop();//尾出队
    9 alert(colors); //hello,blue
    //=======reverse()反转数组 、sort()数组排序=====
    var values = [1,2,3,4,5];
    values.reverse();
    alert(values); //5,4,3,2,1
    
    var values1=[10,8,19,20,3];
    values1.sort();
    alert(values1); //10,19,20,3,8
                //sort()方法会调用每个数组项的toString()转型方法,然后比较。
    //sort()与比较函数一起,可以完成数值排序
    function compare(v1,v2){
        if(v1<v2){
            return -1;
        }else if(v1>v2){
            return 1;
        }else{
            return 0;
        }
    }
    var values2=[10,8,19,20,3];
    values2.sort(compare);
    alert(values2);
    
    //简化compare()函数
    function compare(v1,v2){
        //return v1-v2;//升序
        return v2-v1;//逆序
    }
    var values3=[10,8,19,20,3];
    values3.sort(compare);
    alert(values3);
    //===concat()===不影响原数组的值
    var colors = [1,2,3,4];
    var colors2 = colors.concat("yellow",1 ,2,[3,5]);
    alert(colors.join("-")); //1-2-3-4 ;colors没有变化alert(colors2); //1,2,3,4,yellow,1,2,3,5

    //===slice()====不影响原数组的值 var colors3 = colors.slice(1);//2,3,4 var colors4 = colors.slice(1,3);//2,3 var colors5 = colors.slice(-1); //4 ;取出最后一个元素 alert(colors3); alert(colors4); alert(colors5);
    //===splice()方法
    var arrays=["hello","ok","you",3,4,5,6];
    //1.删除操作:splice(第一项的位置,要删除的项数)
    var removed = arrays.splice(1,2);//删除ok 和 you
    alert(arrays); //hello,3,4,5,6
    alert(removed); //ok,you
    
    //2.插入操作:splice(起始位置,0,要插入项)
    removed = arrays.splice(2,0,"yoyo","good");
    alert(arrays);//hello,3,yoyo,good,4,5,6
    alert(removed instanceof Array); //返回的是空数组
    
    //3.替换操作:splice(起始位置,要删除项,要插入项)
    removed = arrays.splice(1,1,"come","on");
    alert(arrays.join(' '));// hello come on yoyo good 4 5 6
    alert(removed); //3
    //===indexOf()与lastIndexOf()==
    //这两个方法都返回要查找的项在数组中的位置,没有找到的情况下返回-1
    var numbers = ["hello",2,3,"yoyo",47,55,61,17,18,59];
    alert(numbers.indexOf("yoyo")); //3; 正向查找
    alert(numbers.lastIndexOf(18)); //8;从后往前查找
    alert(numbers.indexOf("Good")); //-1
        var person = {name:"Nicolas"}; 
        var person1 = {name:"Yoyo"};
        var people = [{name:"Nicolas"}];
        var morePeople = [person1,person];
        //alert(person instanceof Object); //true; person是一个对象
        //alert(people instanceof Array); //true;  people是一个数组
        //alert(morePeople instanceof Array); //true;morePeople是一个数组
        //alert(people.indexOf(person)); //-1
        alert(morePeople.indexOf(person)); //1

    数组的5个迭代方法:

    //function(item,index,array)三个参数 array[index]=item
    //1.every()
    var numbers = [1,2,3,4,5,6];
    var everyResult = numbers.every(function(item,index,array){
        return (item>2);
    });
    //alert(everyResult); //false;不是数组中的每一项都大于2
    
    //2.map()操作数组中的每一项,最终返回一个数组
    var mapResult = numbers.map(function(item,index,array){
        if(item<4){
            //alert(array[index]); //会打印出当前数组中该索引对应的值
            return item*2;
        }else{
            return item;
        }
    });
    //alert(mapResult); //2,4,6,4,5,6;
    
    //3.filter()按照指定条件过滤数组中的选项
    var filterResult = numbers.filter(function(item){
        if(item%2==0){
            return item;
        }
    });
    //alert(filterResult); //2,4,6
    
    //4.some()只要数组中的选项有一个满足条件,就返回True,否则返回false
    var someResult = numbers.some(function(item,index,array){
        return (item==4);
    });
    //alert(someResult); //true
    
    //5.foreach()该方法没有返回值,本质上与使用for循环迭代数组一样
    var count=0;
    numbers.forEach(function(item,index,array){
        count++;
    });
    //alert(count);
        //ECMAScript5有两个所辖数组的方法:reduce()和reduceRight()。这两个方法都会迭代数组的所有想,然后构建一个最终返回的值。
        //reduce()从数组的第一项开始,逐个遍历到最后.
        //reduceRight()则从数组的最后一项开始,向前遍历到第一项.
        //function(prev,cur,index,array)四个参数:前一个值、当前值、项的索引和数组对象。函数返回的任何值都会作为第一个参数自动传给下一项。
        var  values = [1,2,3,4,5];
        var sum = values.reduce(function(prev,cur,index,array){
            return prev+cur;
        });
        alert(sum);
    //==Date==
    var start = Date.now();
    //alert(start); //输出从1970年1月1日零时开始的毫秒数
    
    var someDate = new Date(Date.parse("May 25,2014"));
    //alert(someDate);
    var someDate2 = new Date(2015,4,25,15,55,55);
    //alert(someDate2.toLocaleDateString()); //2015年5月25日; 月份是基于0的
    //alert(someDate2.toLocaleTimeString());    //13:55:55 ;显示时分秒
    //alert(someDate2.getDate()); //25 ;得到天数
    //alert(someDate2.getDay()); //返回日期中星期的星期几
    //alert(someDate2.getFullYear()); //返回4位数的年份
  • 相关阅读:
    IOS使用正则表达式去掉html中的标签元素,获得纯文本
    iOS 拨打电话的三种方式总结
    iOS中Block的基础用法
    如何避免在Block里用self造成循环引用
    对MAC自带的SVN进行升级
    IOS开发之记录用户登陆状态
    Xcode7 添加PCH文件
    mysql upgrade
    Ubuntu下更改用户名和主机名
    mysql 查询的时候没有区分大小写的解决方案
  • 原文地址:https://www.cnblogs.com/yanyangbyou/p/3955861.html
Copyright © 2011-2022 走看看