JavaScript中一些变态的面试题

 	console.log(parseInt("116",16));
        var aaa = [1,2,3,4,16,17,22];

        var bb = aaa.map(parseInt);//parseInt 只接受两个两个参数 string, radix(基数).如果省略radix(基数或其值为 0,则数字将以 10 为基础来解析。
        // 如果它以 “0x” 或 “0X” 开头,将以 16 为基数。如果该参数小于 2 或者大于 36,则 parseInt() 将返回 NaN。
        console.log(bb);//bb的值为[1,NaN,NaN,1,1,14]

        function test() {
            console.log(a);//undefined
            console.log(foo());//2

            var a = 1;
            function foo() {
                return 2;
            }
        }

        test();

        function test2() {
            console.log(foo);//后面函数
            console.log(bar);//undefined

            var foo = 'Hello';
            console.log(foo);//Hello
            var bar = function () {
                return 'world';
            }

            function foo() {
                return 'hello';
            }
        }

        test2();

        //instanceof 运算符用来检测 constructor.prototype 是否存在于参数 object 的原型链上.
        var c = [typeof null, null instanceof Object];//["object", false]
        console.log(c);

        var val = 'smtg';
        //+ 的优先级 大于 ?
        console.log('Value is ' + (val === 'smtg') ? 'Something' : 'Nothing');//Something



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

        var ary = [0,1,2];
        ary[10] = 10;
        //题目中的数组其实是一个长度为11, 但是没有内容的数组, array 上的操作会跳过这些未初始化的’坑’
        var dd = ary.filter(function(x) { return x === undefined;});
        console.log(dd);//[]


        //switch 是严格比较, String 实例和 字符串不一样.
        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!');
            }
        }
        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
        showCase(new String('A'));//Do not know!


        console.log(parseInt(3,8));//3
        console.log(parseInt(3,2));//NaN
        console.log(parseInt(3,0));//3

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

        var ary = Array(3);
        ary[0]=2;
        //题目中的数组其实是一个长度为3, 但是没有内容的数组, array 上的操作会跳过这些未初始化的’坑’
        var arr = ary.map(function(elem) { return '1'; });
        console.log(arr);//["1", undefined × 2]

        console.log(Number.MAX_VALUE);//1.7976931348623157e+308
        console.log(Number.MIN_VALUE);//5e-324
        console.log(Number.MIN_VALUE > 0);//true
        var a2 = new Number(123);
        console.log(a2.valueOf());

        console.log("============================================================");
        var a = /123/;
        var b = /123/;
        console.log(a == b);//false   即使正则的字面量一致, 他们也不相等.
        console.log(a === b);//false


        //只有 Function 拥有一个 prototype 的属性. 所以 a.prototype 为 undefined.
        //而 Object.getPrototypeOf(obj) 返回一个具体对象的原型(该对象的内部[[prototype]]值)
        var a = {},b = Object.prototype;
        var arr = [a.prototype === b,Object.getPrototypeOf(a) === b];
        console.log(arr);//false,true


        //f.prototype 是使用 new 创建的 f 实例的原型. 而 Object.getPrototypeOf 是 f 函数的原型
        function f() {}
        var a = f.prototype, b = Object.getPrototypeOf(f);
        console.log(a === b);//
        console.log(a === Object.getPrototypeOf(new f()));//true
        console.log(b === Function.prototype);//true

        function foo() { }
        var oldName = foo.name;
        foo.name = "bar";
        console.log([oldName, foo.name]);//["foo", "foo"]  因为函数的名字不可变.


        function f() {}
        var parent = Object.getPrototypeOf(f);
        console.log(f.name); // 'foo'
        console.log(parent.name); // ''
        console.log(typeof eval(f.name)); // function
        console.log(typeof eval(parent.name)); //  undefined


        var a = Function.length,b = new Function().length
        //function(Function 的实例)的 length 属性就是函数签名的参数个数.所以b.length=0 Function.length 定义为1
        console.log(a === b);//false


        var a = Date(0);
        var b = new Date(0);
        var c = new Date();
        console.log(a);
        console.log(b);
        console.log(c);
        console.log(typeof a);//String  如果是函数调用 返回一个字符串.
        console.log(typeof b);//object
        console.log(typeof c);//object

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值