前端開發(fā)月薪30K必看面試題大全(2)

第9題

function showCase(value) {
    switch(value) {
    case 'A':
        console.log('Case A');
        break;
    case 'B':
        console.log('Case B');
        break;
    case undefined:
        console.log('undefined');
        break;
    default:
        console.log('Do not know!');
    }
}
showCase(new String('A'));

兩個(gè)知識(shí)點(diǎn):

Statements/switch
String

switch 是嚴(yán)格比較, String 實(shí)例和 字符串不一樣.

var s_prim = 'foo';
var s_obj = new String(s_prim);
 
console.log(typeof s_prim); // "string"
console.log(typeof s_obj);  // "object"
console.log(s_prim === s_obj); // false

答案是 'Do not know!'

第10題

function showCase2(value) {
    switch(value) {
    case 'A':
        console.log('Case A');
        break;
    case 'B':
        console.log('Case B');
        break;
    case undefined:
        console.log('undefined');
        break;
    default:
        console.log('Do not know!');
    }
}
showCase2(String('A'));

解釋:
String(x) does not create an object but does return a string, i.e. typeof String(1) === "string"

還是剛才的知識(shí)點(diǎn), 只不過 String 不僅是個(gè)構(gòu)造函數(shù) 直接調(diào)用返回一個(gè)字符串哦.

答案 'Case A'

第11題

function isOdd(num) {
    return num % 2 == 1;
}
function isEven(num) {
    return num % 2 == 0;
}
function isSane(num) {
    return isEven(num) || isOdd(num);
}
var values = [7, 4, '13', -9, Infinity];
values.map(isSane);

一個(gè)知識(shí)點(diǎn)

Arithmetic_Operators#Remainder

此題等價(jià)于

7 % 2 => 1
4 % 2 => 0
'13' % 2 => 1
-9 % % 2 => -1
Infinity % 2 => NaN

需要注意的是 余數(shù)的正負(fù)號(hào)隨第一個(gè)操作數(shù).
答案 [true, true, true, false, false]

第12題

parseInt(3, 8)
parseInt(3, 2)
parseInt(3, 0)

第一個(gè)題講過了, 答案 3, NaN, 3

第13題

Array.isArray( Array.prototype )

一個(gè)知識(shí)點(diǎn):
Array/prototype
一個(gè)鮮為人知的實(shí)事: Array.prototype => [];
答案: true

第14題

var a = [0];
if ([0]) {
  console.log(a == true);
} else {
  console.log("wut");
}

JavaScript-Equality-Table

答案: false

第15題

[]==[]

== 是萬惡之源, 看上圖

答案是 false

第16題

'5' + 3
'5' - 3

兩個(gè)知識(shí)點(diǎn):

Arithmetic_Operators#Addition
Arithmetic_Operators#Subtraction

  • 用來表示兩個(gè)數(shù)的和或者字符串拼接, -表示兩數(shù)之差.

請(qǐng)看例子, 體會(huì)區(qū)別:

'5' + 3
'53'
5 + '3'
'53'
5 - '3'
2
'5' - 3
2
'5' - '3'
2

也就是說 - 會(huì)盡可能的將兩個(gè)操作數(shù)變成數(shù)字, 而 + 如果兩邊不都是數(shù)字, 那么就是字符串拼接.

答案是 '53', 2

第17題

1 + - + + + - + 1

這里應(yīng)該是(倒著看)

1 + (a)  => 2
a = - (b) => 1
b = + (c) => -1
c = + (d) => -1
d = + (e) => -1
e = + (f) => -1
f = - (g) => -1
g = + 1   => 1

所以答案 2

第18題

var ary = Array(3);
ary[0]=2
ary.map(function(elem) { return '1'; });

稀疏數(shù)組. 同第7題.

題目中的數(shù)組其實(shí)是一個(gè)長(zhǎng)度為3, 但是沒有內(nèi)容的數(shù)組, array 上的操作會(huì)跳過這些未初始化的’坑’.

所以答案是 ["1", undefined × 2]

這里貼上 Array.prototype.map 的 polyfill.

Array.prototype.map = function(callback, thisArg) {
 
        var T, A, k;
 
        if (this == null) {
            throw new TypeError(' this is null or not defined');
        }
 
        var O = Object(this);
        var len = O.length >>> 0;
        if (typeof callback !== 'function') {
            throw new TypeError(callback + ' is not a function');
        }
        if (arguments.length > 1) {
            T = thisArg;
        }
        A = new Array(len);
        k = 0;
        while (k < len) {
            var kValue, mappedValue;
            if (k in O) {
                kValue = O[k];
                mappedValue = callback.call(T, kValue, k, O);
                A[k] = mappedValue;
            }
            k++;
        }
        return A;
    };

第19題

function sidEffecting(ary) {
  ary[0] = ary[2];
}
function bar(a,b,c) {
  c = 10
  sidEffecting(arguments);
  return a + b + c;
}
bar(1,1,1)

這是一個(gè)大坑, 尤其是涉及到 ES6語法的時(shí)候

知識(shí)點(diǎn):
Functions/arguments
首先 The arguments object is an Array-like object corresponding to the arguments passed to a function.

也就是說 arguments 是一個(gè) object, c 就是 arguments[2], 所以對(duì)于 c 的修改就是對(duì) arguments[2] 的修改.

所以答案是 21.

然而!!!!!!
當(dāng)函數(shù)參數(shù)涉及到 any rest parameters, any default parameters or any destructured parameters 的時(shí)候, 這個(gè) arguments 就不在是一個(gè) mapped arguments object 了…..

請(qǐng)看:

function sidEffecting(ary) {
  ary[0] = ary[2];
}
function bar(a,b,c=3) {
  c = 10
  sidEffecting(arguments);
  return a + b + c;
}
bar(1,1,1)

答案是 12 !!!!
請(qǐng)讀者細(xì)細(xì)體會(huì)!!

第20題

var a = 111111111111111110000,
    b = 1111;
a + b;

答案還是 111111111111111110000. 解釋是 Lack of precision for numbers in JavaScript affects both small and big numbers. 但是筆者不是很明白……………. 請(qǐng)讀者賜教!

第21題

var x = [].reverse;
x();

這個(gè)題有意思!
知識(shí)點(diǎn):
Array/reverse

The reverse method transposes the elements of the calling array object in place, mutating the array, and returning a reference to the array.

也就是說 最后會(huì)返回這個(gè)調(diào)用者(this), 可是 x 執(zhí)行的時(shí)候是上下文是全局. 那么最后返回的是 window.

答案是 window

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容