05_Javascript进阶第二天

时间:2023-03-09 01:36:43
05_Javascript进阶第二天
  1. String对象
    res=str.charAt(1);//返回字符串中第n个字符(输出:i)
    res=str.charCodeAt(1);//返回第n个字符的ASCII编码(输出:105)
    res=String.fromCharCode(105,97);//根据ASCII编码返回指定字符,可指定多个(输出:ia)
    res=str.concat('yes');//连接字符串,相当于连接符+(输出:Will you set sail tonight?yes)
    res=str.indexOf('you',3);//查找字符串'you'首次出现的位置,指定从下标3开始查找(输出:5)
    //通过indexOf可以统计一个字符在字符串中出现的次数
    function _count(str,search){
    var count=0,n=0;
    while(n!=-1){
    count++;
    n=str.indexOf(search,n+1);
    }
    return count;
    }
    var num=_count('abcaabs','a');
    console.log('count:'+num); res=str.lastIndexOf('you');//返回查找字符最后一次出现的位置,实际上是从后面开始查找(输出:25)

    sort的排序原理是什么?

    //localCpmpare比较字符串(实现中文按照字母排序)
    var array = ['白鸽', '麻雀', '大象', '狗', '猫', "鸡"];
    array = array.sort(
    function compareFunction(item1, item2) {
    return item1.localeCompare(item2);
    }
    );
    //输出:"白鸽,大象,狗,鸡,麻雀,猫"
    var arr=[6,1,3,5,2,8]
    arr=arr.sort(
    function(x,y){
    return x>y;//从小到大排序
    }
    )
    输出:"1,2,3,5,6,8"

    match匹配,search搜索,replace代替

    var str='this is a test';
    var res;
    res=str.match('is');//输出:["is", index: 2, input: "this is a test"]
    res=str.search('is');//输出:2
    res=str.replace(/is/g,'?');//全局匹配。输出:"th? ? a test",如果不加g,只替换第一个is
    str='2017-12-9';
    res=str.replace(/(\d{4})-(\d{2})-(\d{1,2})/,'$2/$3/$1');//输出:"12/9/2017"
    //用函数实现
    res=str.replace(/(\d{4})-(\d{2})-(\d{1,2})/,func);
    function func(match,d1,d2,d3){
    // return d2+'/'+d3+'/'+d1;
    return [d2,d3,d1].join('/'); }
    console.log(res);

    字符串截取

            var str='abcde';
    var res;
    //slice(start.end),返回截取后的字符串
    //substr(start,length),返回截取之后的字符串
    //split(delimiter[,limit]),将字符串拆分为数组
    res=str.slice(0,-1);//输出:abcd
    res=str.substr(2,2);//输出:cd str='red,green,blue';
    res=str.split(',',2);//2是可选参数,表示返回两个元素,输出:"red,green"

    其他

            //字符串大小写相关
    str='HELLO world';
    res=str.toLowerCase();//换成小写
    res=str.toLocaleLowerCase();//也是换成小写
    res=str.toUpperCase();//大写 //trim字符串过滤
    str=' hello world ';
    res=str.trim();//去掉了前后的空格 //产生锚点
    str='this is a test';
    document.body.innerHTML=str.anchor('anchor_name');//<a name="anchor_name">this is a test</a>
    //产生链接
    var title='百度';
    var url="http://www.baidu.com";
    document.write(title.link(url));\n
    //<a href="http://www.baidu.com">百度</a>
  2. Function对象
    属性:
        var res;
    function a(x,y,z){
    return x+y+z;
    }
    //1、constructor返回创建该对象的构造函数
    res=a.constructor;//输出:function Function() { [native code] } //2、length返回函数的参数个数
    res=a.length;//输出:3 //3、caller返回调用当前函数的函数
    function f1(){
    return f1.caller;
    }
    function f2(){
    return f1();
    }
    res=f2();
    //输出:"function f2(){return f1();}" //4、arguments返回由函数的所有参数组成的数组
    function a(){
    return arguments;
    }
    res=a(1,2,'c');//输出:[1, 2, "c", callee: function, Symbol(Symbol.iterator): function] //arguments有个callee属性,返回当前被调用的函数对象 function a(){
    return arguments.callee;
    }
    res=a();
    //输出:function a(){……}
    //可以利用callee属性实现匿名函数的自身调用
    (function(count){
    if(count<=3){
    alert(count);
    arguments.callee(++count);
    }
    })(0);

    call和apply方法

        //call回调函数
    var obj={
    say:function(x,y){return x+y;}
    };
    var obj1={};
    res=obj.say.call(obj1,1,2);//obj1调用obj的方法,1,2是参数
    res=obj.say.apply(obj,[2,3]);//跟call方法差不多,只不过参数以数组形式传递
  3. Math对象,不是函数对象
            var res;
    res=Math.E;//输出: "2.718281828459045"
    res=Math.PI;//输出:"3.141592653589793"
    res=Math.abs(-123);//取绝对值,输出:123
    res=Math.ceil(2.14);//向上取整,输出:2
    res=Math.floor(2.82);//向下取整,输出:2
    res=Math.round(2.45);//四舍五入,输出:2
    res=Math.pow(2,3);//2的3次方根,输出:8
    res=Math.sqrt(16);//开方根,输出:4
    res=Math.max(1,45,6);//求最大值,输出:45
    console.log(res)
  4. 对象的原型prototype
        function product(name,color){
    this.name=name;
    this.color=color;
    this.say=function(){return 'this is a test';};
    }
    product.prototype.price=123;
    product.prototype.memory=32;
    var p1=new product('苹果手机','白色');
    //new运算符,函数里面的this自动指向创造的对象,product函数里面的this指代p1,
    //所以p1有3个自身属性,price和memory是原型链的属性
    for(var i in p1){
    //判断是否是自身属性
    if(p1.hasOwnProperty(i)){
    console.log(p1[i]);//不能用p1.i
    } }
  5. 内建对象的扩展
    String.prototype.reverse=function(){
    return Array.prototype.reverse.apply(this.split('')).join('');
    }
    console.log('abc'.reverse());//输出:cba //检测方法是否存在,不存在则扩展
    if(!Array.prototype.inArray){
    // alert('no');
    Array.prototype.inArray=function(search){
    for(var i=0;i<this.length;i++){
    if(this[i]==search){
    return true;
    }
    }
    return false;
    }
    } var arr=['a','b','c'];
    console.log(arr.inArray('A'));
  6. 13章后看不下去,继承,原型链什么的……