渡一——*******&&习题整理&&********

1.传字符串返回字节长度

1-1.当前字符位的unicode > 255,那么该字符字节长度为2;
1-2.<255为1;

//1-1
var str = "adfsf;fdkdsflwe";
function bytesLen(str){
    var len = str.length;//默认都是1,后面找是2的就加1
    for(var i=0;i<str.length;i++){
        if(str.charCodeAt(i)>255){
            len++
        }
    }
    return len;
}
//1-2
function bytesLen2(str){
    var len = 0;
    for(var i=0;i<str.length;i++){
        //默认是0,后面判断是加几
        str.charCodeAt(i) > 255 ? count += 2 : count++;
    }
    return len;
}

 

可正常计算的范围
前17后17位
小数点前17位,后17位

10000000000000001 + 10000000000000001        //小数点前17位
0.10000000000000001 + 0.10000000000000001   //小数点后17位数

 

面视题整理

作用域

1.

window.onload = function(){
    var length = 88;
    function test(){
        console.log(this.length);
    }
    var obj = {
        length:99,
        action:function(test){
            test();             //此处没有调用者,this-->window
            arguments[0]();     //arguments有两个参数,arguments[0]就是test函数,this-->[test,[1,2,3]],this.length=2;
        }
    }
    obj.action(test,[1,2,3]);
}

var f = obj.action;
f();    //this.length-->88;这里还是全局调用

function f(){
    console.log(this) //[f,2,3]
};
var arr = [f,2,3];
arr[0]();
var f1 = arr[0];
f1()    //window

2.

//2-1.
var a=10;
function test(){
    console.log(a);     //undefined 
    a = 100;
    console.log(this.a);//this指向window 10,这里调用都是window
    var a;
    console.log(a);     //100 
}
test();

//2-2
var a=10;
function test(){
    console.log(a);     //这里只有全局变量a 10 
    a = 100;
    console.log(this.a);//100
    console.log(a);     //100 
}
test();

//2-3
var d = 100;
function f4(){
    var d = 200;
    console.log(this.d); //100 this指向window
}
f4();

3.

var a = 10;
function f(){
    var b = 2 * a;  //没有this.a 就只取局部变量a,这里a:undefined; b=2*undefined:NaN
    var a = 20;
    var c = a + 1;  //21
    console.log(b);
    console.log(c);
}
f1();

 

posted @ 2021-09-18 10:02  lisa2544  阅读(45)  评论(0编辑  收藏  举报