javascript底层练习

1.请看下列代码:

function F(){
    function C(){
    return this;
  }
  return C();
}
var o=new F();

请问上面的this值指向的是全局对象还是对象o?

答:全局对象。

function C(){
    return this;
}

这里面this指向了全局对象, 

在return C();时,调用C方法,返回了全局对象,
然后return了全局对象。
在new构造函数时,如果构造函数没有return语句,返回的是构造函数的this,如果有return语句,并且return的是一个对象,那么new出来的就是return的这个对象;如果return一个基本类型的对象,那么new出来还是构造函数的this。
所以在最后return C()的时候相当于return 全局对象,全局对象不是基本类型,所以o指向的全局对象。

2.下面代码的执行结果会是什么?

function C(){
    this.a=1;
  return false;
}
console.log(typeof new C());

跟上面这个题考点一样啊。。。
输出object

3.下面这段代码的执行结果又将是什么?

var c=[1,2,[1,2]];
c.sort();
c.join("--");
console.log(c);
  • 1
  • 2
  • 3
  • 4

答:[1,[1,2],2]
sort完以后数组变成这样,join方法不改变数组。
如果题目是

var c=[1,2,[1,2]];
c.sort();
c=c.join("-");
console.log(c);

最后输出join的结果,是 1-1,2-2 

这个结果好有迷惑性,如果没想通仔细想一下,我是晕了会才发现就这样啊。。。

4.在String()构造函数不存在的情况下自定义一个MyString()的构造器函数。记住,由于String()不存在,因此您在写构造器函数时不能使用任何属于内建String对象的方法和属性。并且要让您所创建的对象通过以下测试:

var s = new MyString("hello");
s.length;  //5

s[0];  //"h"

s.toString();  //"hello"

s.valueOf();  //"hello"

s.charAt(1);  //"e"

s.charAt("2");  //"l"

s.charAt("e");  //"h"

s.concat(" world!");  //"hello world!"

s.slice(1,3);  //"el"

s.slice(0,-1);  //"hell"

s.split("e");  //["h","llo"]

s.split("l");  //["he","","o"]

如果您觉得这个练习很有趣,可以不用止步于join()方法,继续为其创建尽可能多的方法。

参考答案:

    function MyArray(){
        this.length=arguments.length;
        for(var i=0;i<this.length;i++){
            this[i]=arguments[i];
        }

        this.toString=function(){
            var resultStr="";
            for(var i=0;i<this.length;i++){
                if(i===this.length-1){
                    resultStr+=this[i].toString();
                }else{
                    resultStr+=this[i].toString()+",";
                }
            }
            return resultStr;
        };
        this.push=function(obj){
            this[this.length]=obj;
            this.length++;
            return this.length;
        };
        this.pop=function(){
            if(this.length===0){
                return null;
            }
            result=this[this.length-1];
            this[this.length-1]=undefined;
            this.length--;
            return result;
        };
        this.join=function(str){
            var resultStr="";
            for(var i=0;i<this.length;i++){
                if(i===this.length-1){
                    resultStr+=this[i].toString();
                }else{
                    resultStr+=this[i].toString()+str;
                }
            }
            return resultStr;
        }
    }

7.在Math对象不存在的情况下,创建一个类似的MyMath对象,并为其添加以下方法:
1)MyMath.rand(min,max,inclusive)-随机返回min到max区间中的一个数,inclusive为true时为闭区间(这也是默认情况)。
2)MyMath.min(array)-返回目标数组中的最小值。
3)MyMath.Max(array)-返回目标数组中的最大值。

参考答案:

    function MyMath(){

    }
    MyMath.rand=function(min,max,inclusive){
        if(typeof min!=="number"){
            throw new Error("type error");
            return;
        }
        if(typeof max!=="number"){
            throw new Error("type error");
            return;
        }
        if(min>max){
            throw new Error("parameter error");
            return;
        }
        if(typeof inclusive==="undefined"){
            inclusive=true;
        }else{
            inclusive=!!inclusive;
        }
        if(inclusive){
            if(Math.random()>0.5){
                return min+(max-min)*Math.random();
            }else{
                return max-(max-min)*Math.random();
            }
        }else{
            //不闭合区间,先排除0的可能
            var randomNum=Math.random();
            while(randomNum===0){
                randomNum=Math.random();
            }
            return min+(max-min)*randomNum;
        }
    };
    MyMath.min=function(){
        if(arguments.length===0){
            throw new Error("no parameter");
            return;
        }
        var minValue;
        var l=arguments.length;
        for(var i=0;i<l;i++){
            var param=arguments[i];
            if(typeof param!=="number"){
                throw new Error("parameter error");
                return;
            }
            if(i===0){
                minValue=param;
            }
            if(minValue>param){
                minValue=param;
            }
        }
        return minValue;
    };
    MyMath.max=function(){
        if(arguments.length===0){
            throw new Error("no parameter");
            return;
        }
        var maxValue;
        var l=arguments.length;
        for(var i=0;i<l;i++){
            var param=arguments[i];
            if(typeof param!=="number"){
                throw new Error("parameter error");
                return;
            }
            if(i===0){
                maxValue=param;
            }
            if(maxValue<param){
                maxValue=param;
            }
        }
        return maxValue;
    };
原文地址:https://www.cnblogs.com/hngdlxy143/p/9736620.html