你需要知道的25个JavaScript面试题

匿名网友 匿名网友 发布于: 2015-12-31 00:00:00
阅读 127 收藏 0 点赞 0 评论 0

1.What is a potential pitfall with using “typeof bar === ‘object'” to determine if “bar” is an object? How can this pitfall be avoided?(使用”typeof bar === ‘object'” 判断 bar是object的潜在危险,如何避免)

// null also considered an object
var bar = null;
console.log(typeof bar === "object");  // logs true

// 判断是否为对象时,先判断 bar 是否为 null,即可避免
console.log((bar !== null) && (typeof bar === "object")); 

// There are two other things worth nothing:
// First, the above solution will return "false" if "bar" is a function. If you wanted to also return "true" for functions, you could amend the above solution to be:
console.log((bar !== null) && ((typeof bar === "object") || (typeof bar === "function")));

// Second, the above solution will return "true" if "bar" is an array. If you want to also false for arrays, you could amend the above solution to be:
console.log((bar !== null) && (typeof bar === "object"));  // true
console.log((bar !== null) && (typeof bar === "object") && (toString.call(bar) !== "[object Array]")); // false

// jquery
console.log((bar !== null) && (typeof bar === "object") && (!$.isArray(bar)));

2.What will the code below output to the console and why?

(function () {
            var a = b = 3;
        })();
        console.log("a defined? " + (typeof a !== "undefined"));
        console.log("b defined? " + (typeof b !== "undefined"));
    The answer:
        console.log("a defined? " + (typeof a !== "undefined"));  // false
        console.log("b defined? " + (typeof b !== "undefined"));  // true
        // In fact, var a = b = 3: is actually shorthand for:
        // b = 3;
        // var a = b;
        // b ends up being a global variable

3.What will the code below output to the console and why?

        var myObject = {
            foo: "bar",
            func: function () {
                var self = this;
                console.log("outer func: this.foo = " + this.foo);
                console.log("outer func: self.foo= " + self.foo);
                (function () {
                    console.log("inner func: this.foo = " + this.foo);
                    console.log("inner func: this.foo = " + self.foo);
                }());
            }
        }
        myObject.func();

The answer:

// The above code will output the following to the console
        // outer func: this.foo = bar
        // outer func: self.foo = bar 
        // inner func: this.foo = undefined
        // inner func: self.foo = bar
        // In the outer function, both "this" and "self" refer to "myobject"
        // In the inner function, "this" no longer refers to "myobject","self" refer to "myobject"

4.What is the significance of, and reason for, wrapping the entire content of a JavaScript source file in a function block? The answer: This is an increasingly common practice, employed by many popular JavaScript libraries (jQuery, Node.js, etc.). This technique creates a closure around the entire contents of the file which, perhaps most importantly, creates a private namespace and thereby helps avoid potential name clashes between different JavaScript modules and libraries. (单独的命名空间,避免命名冲突)。

5.What is the significance, and what are the benefits, of including “use strict” at the beginning of a JavaScript source file?
The answer: 设立“严格模式”的目的,主要有以下几个: -消除JavaScript语法的一些不合理、不严谨之处,减少一些怪异模式; -消除代码运行的一些不安全之处,保证代码运行的安全; -提高编译器效率,增加运行速度; -为未来新版本的JavaScript做好铺垫。 // 进入“严格模式”的标志,是:”use strict” 语法和行为改变: -全局变量显式声明..etc..; 参考地址

  1. Consider the two functions below. Will they both return the same thing? Why or why not?
function foo1() {
            return {
                bar: "hello"
            };
        }

        function foo2() {
            return
            {
                bar: "hello"
            };
        }
        window.onload = function () {
            console.log(foo1());  // bar: "hello"
            console.log(foo2());  // undefined
        }
  semicolons(分号) are technically optional in JavaScript 
  1. What is “NaN”? What is the type? How can you reliably test if a value is equal to “NaN”?
        // means not a number

        // type: number
        // Additionally, "NaN" compared to anything - even itself! - is false:
        window.onload = function () {
            console.log(typeof NaN === "number"); // logs true
            console.log(NaN === NaN); // logs false
        }

        // test if a value is equal to "NaN"
        // 1. isNaN();
        // 2. value !== value  // logs true

8.What will the code below output? Explain your answer?

        window.onload = function () {
            console.log(0.1 + 0.2);  // 0.30000000000000004
            console.log(0.1 + 0.2 == 0.3); // false
        }
        // 数字在JavaScript中都是浮动精度的处理 

评论列表
文章目录