js中一些有意思的题

javascript变态解析题

今天在车上无聊时看到了一篇文章关于javascript的变态解析题,看了看发现自己智商都不够用了。
哈哈。。。今天特地贴出来留着以后自己在看看。

第一题:

    [1,2,3].map(parseInt):

知识点:

  • Array/map
  • Number/parseInt
  • JavaScript parseInt

首先,map接受两个参数,一个回调函数callback,一个回调函数的this值。其中回调函数接受三个参数currentValue,index,array.
而题目中,map只传入了回调函数-parsetInt.
其次,parsetInt只接受两个参数string,radix(基数).
string:必需。要被解析的字符串。
radix:可选。表示要解析的数字的基数。该值介于 2 ~ 36 之间。
如果省略该参数或其值为 0,则数字将以 10 为基础来解析。如果它以 “0x” 或 “0X” 开头,将以 16 为基数。
如果该参数小于 2 或者大于 36,则 parseInt() 将返回 NaN。
本题即等同于:

parseInt("1",0);
parseInt("2",1);
parseInt("3",2);

后两者参数参数不合法。
所以答案是:[1,NaN,NaN];

第二题:

[typeof null,null instanceof Object]

知识点:

  • Operators/typeof
  • Operators /instanceof
  • Operators /instanceof(中)

    typeof返回一个表示类型的字符串.
    instanceof运算符用来检测constructor.prototype是否存在于参数object的原型链上.

对变量或值调用 typeof 运算符将返回下列值之一:

  • undefined - 如果变量是 Undefined 类型的
  • boolean - 如果变量是 Boolean 类型的
  • number - 如果变量是 Number 类型的
  • object - 如果变量是一种引用类型或 Null类型的

因此答案[object,false]

第三题:

[[3,2,1].reduce(Math.pow),[].reduce(Math.pow)]

知识点:

  • Array/Reduce
array1.reduce(callbackfn[, initialValue])

array1:必需。一个数组对象。
callbackfn: 必需。一个接受最多四个参数的函数。对于数组中的每个元素,reduce 方法都会调用 callbackfn 函数一次。
initialValue:可选。如果指定 initialValue,则它将用作初始值来启动累积。第一次调用 callbackfn 函数会将此值作为参数而非数组值提供。


回调函数有4个参数:previousValue、currentValue、currentIndex、array1
previousValue:通过上一次调用回调函数获得的值。如果向 reduce 方法提供 initialValue,则在首次调用函数时,previousValue为 initialValue。
currentValue:当前数组元素的值。
currentIndex:当前数组元素的数字索引。
array1:包含该元素的数组对象。

当满足下列任一条件时,将引发TypeError异常:

  • callbackfn 参数不是函数对象。
  • 数组不包含元素,且未提供 initialValue。

所以第二个表达式会报异常。第一个表达式等价于Math.pow(3,2)=>9;Math,pow(9,1)=>9

答案 an error

第四题

var val='smtg';
console.log('Value is'+(val==='smtg')?'Something':'Nothing');

知识点

  • Operators/Opertor_Precedence
  • Operators/Conditional_Opertor
    即+的优先级大于?

所以原题等价于 ‘Value is true’?’Something’:’Nothing’而不是’Value is’+(true?’Something’:’Nothing’)

答案: ‘Something’

第五题

var name='World';
(function(){
    if(typeof name === 'undefined'){
        var name = 'Jack';
        console.log('Goodbye'+name);
    }else{
        console.log('Hello'+name);
    }
})()

知识点

  • Hoisting

在jacascript中,funcions和variables会被提升。变量提升是javascript将声明移至作用域scope(全局域或者当前函数作用域)顶部的行为。
这题目相当于:

var name='World';
(function(){
    var name;
    if(typeof name === 'undefined'){
        name = 'Jack';
        console.log('Goodbye '+name);
    }else{
        console.log('Hello '+name);
    }
})()

所以答案是 ‘Goodbye Jack’

第六题

var END = Math.pow(2,53)
var START = END - 100;
var count = 0;
for(var i=STRAT;i<=END;i++){
    count++;
}
console.log(count)

知识点

  • Infinity

在js里,Math.pow(2,53)==9007199254740992是可以表示的最大值.最大的值加一还是最大值.所以循环不会停.

第七题:

var two = 0.2;
var one = 0.1;
var eight = 0.8;
var six = 0.6;
[two-one == one,eight-six == two]

答案是[true,false]
为什么是这个答案我也不知道,只能说是javascript的设计缺陷, 浮点运算0.1+0.2!=0.3

第八题:

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.lig('Do not know!');
    }
}
showCase(new String('A');

知识点

  • Statements/switch
  • String
var s_str = 'foo';
var s_obj = new String(s_str);
console.log( s_str);//"string"
console.log( s_obj);//"object"
console.log( s_str===s_obj);//"false"

答案’Do not know!’

第九题

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.lig('Do not know!');
    }
}
showCase2( String('A');

还是刚才的知识点,只不过String直接调用返回一个字符串。
答案’Case A’

第十题:

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

答案3,NaN,3

第十一题:

Array.isArray(Array.prototype)

知识点

  • Array/prototype
    貌似是一个鲜为人知的实事:Array.prototype=>[]; (又涨知识了)
    答案:true
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值