javascript面試題刷

javascript前端面試題彙總

1、 JavaScript中如何檢測一個變量類型是String?請寫出函數實現

//分析:String的兩種創建方法:
//第一種方法:
var str = "str" //str只是一個以String爲數據類型的值,但並不屬於String對象的實例
//第二種方法:
var strObj = new String("strObj") //此時的strObj是String對象的一個實例

//針對第一種創建方式,採用typeof檢測,此時採用instanceof != String
//針對第二種創建方式,採用instanceof檢測,此時採用typeof檢測出來的是Object

function isString(str){
    return (typeof str).toLowerCase() === 'string' || str instanceof String
}

2、原型和原型鏈經典題目

function Foo(){
    getName = function(){ alert(1)}
}

Foo.getName = function(){alert(2)}

Foo.prototype.getName = function(alert(3))

var getName = function(){alert(4)}

function getName(){alert(5)}


//問題:請給出下面運行的結果
Foo.getname();
getName();
Foo().getName();
getName();
New Foo.getName();
new Foo().getName();
new new Foo().getName();

進入環境(代碼未執行,已編譯):

VO:{
    Foo:{
    <reference to function>
    getName:<reference to function(){alert(1)}>
    return this
    }
    getName:<reference to function(){alert(5}>
    
}

代碼執行1Foo.getName()

VO:{
    Foo:{
    <reference to function>,
    getName:<reference to function(){alert(2)}>,
    return this
    },
    getName:<reference to function(){alert(5)}>
}

代碼執行2Foo.prototype.getName = function(){alert(3)}

VO:{
    Foo:{
    <reference to function>
    getName:<reference to function(){alert(2)}>,
    prototype:{
        getName:<reference to function(){alert(3)}>
    }
    return this
    }
    getName:<reference to function(){alert(5)}>
}

代碼執行3var getName = function(){alert(4);};

VO:{
    Foo:{
    <reference to function>
    getName:<reference to function(){alert(2)}>,
    prototype:{
        getName:<reference to function(){alert(3)}>
    }
    return this
    }
    getName:<reference to function(){alert(4)}>
}

代碼執行4Foo.getName()

VO:{
    Foo:{
    <reference to function>
    getName:<reference to function(){alert(2)}>,
    prototype:{
        getName:<reference to function(){alert(3)}>
    }
    return this
    }
    getName:<reference to function(){alert(4)}>
}

代碼執行5getName() //2
代碼執行6Foo().getName()

Foo().getName() == window.getName()
//同時注意:這裏由於Foo()調用,導致VO發生了變化。最後alert(1)
VO:{
    Foo:{
    <reference to function>
    getName:<reference to function(){alert(2)}>,
    prototype:{
        getName:<reference to function(){alert(3)}>
    }
    return this
    }
    getName:<reference to function(){alert(1)}>
}

代碼執行7getName() //1
代碼執行8,9,10

//調用優先順序
成員訪問 > new(帶參數列表)>函數調用>new(無參數列表)

3、this

var name = 'the window'
var obje = {
    name:'myObject',
    getNameFunc:function(){
        return function(){
            return this.name
        }
    }
}
obje.getNameFunc()()

4、查找一個字符串中指定字符出現的位置

var stringValue = 'lorem ipsum dolor sit amet ,consectent adipisicing elit'
var array = []
var pos = stringValue.indexOf('e')
while(pos > -1){
    array.push(pos)
    pos = stringValue.indexOf('e',++pos)
}

5、this經典問題

var a =1;
function foo(a,b){
    a = 2;
    console.log(a);
    var a;
    console.log(a);
    arguments[0] = 3
    console.log(a,this.a,b)
}
//2
//2
//2 1 undefined
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章