栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > Java面试题

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

Java面试题 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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 objectvar 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"));  // trueconsole.log((bar !== null) && (typeof bar === "object") && (toString.call(bar) !== "[object Array]")); // false// jqueryconsole.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中都是浮动精度的处理 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/359253.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号