Home  >  Article  >  Web Front-end  >  Detailed analysis of code examples for 14 tough JavaScript interview questions

Detailed analysis of code examples for 14 tough JavaScript interview questions

黄舟
黄舟Original
2017-03-06 14:49:091393browse

Front-end engineers sometimes encounter a type of interviewer during interviews. The questions they ask are very realistic about the language itself and are often not the practical questions that candidates may expect (some candidates emphasize that As long as you can work, it's irrelevant whether you know the reason or not). Although this type of question has no logic, to a certain extent, it does test the candidate's understanding of the javascript language.

I suddenly thought of this topic because I was looking through my Github out of boredom to see what ugly things I had written before. Then I came across this article quiz-legend that explains the Javascript quiz. Anyway, it’s okay, so I wanted to move it here for everyone to learn, understand, recite, and criticize.

Question 1

(function(){
    return typeof arguments;//"object"
})();

arguments is an Array-like object, corresponding to the parameter list passed into the function. You can use this variable directly in any function. The

typeof operator will only return results of type string. Refer to the following list to know what the values ​​returned by typeof are for different data:

Type Result
undefined 'undefined'
null 'object'
Boolean 'boolean'
Number 'number'
String 'string'
Symbol (new in ECMAScript 2015) ' symbol'
Host object (provided by the JS environment) Implementation-dependent
Function object ( implements [[Call]] in ECMA-262 terms) 'function'
Any other object 'object'

由此我们推断出,typeof argumentsobject

问题二

var f = function g(){ return 23; };
typeof g();//报错

这是一个名字是g的function expression,然后又被赋值给了变量f

这里的函数名g和被其赋值的变量f有如下差异:

  • 函数名g不能变动,而变量f可以被重新赋值

  • 函数名g只能在函数体内部被使用,试图在函数外部使用g会报错的

问题三

(function(x){
    delete x;
    return x;//1
})(1);

delete操作符可以从对象中删除属性,正确用法如下:

delete object.property
delete object['property']

delete操作符只能作用在对象的属性上,对变量和函数名无效。也就是说delete x是没有意义的。

你最好也知道,delete是不会直接释放内存的,她只是间接的中断对象引用

问题四

var y = 1, x = y = typeof x; x;//"undefined"

我们试图分解上述代码成下面两步:

var y = 1; //step 1
var x = y = typeof x; //step 2

第一步应该没有异议,我们直接看第二步

  1. 赋值表达式从右向左执行

  2. y被重新赋值为typeof x的结果,也就是undefined

  3. x被赋值为右边表达式(y = typeof x)的结果,也就是undefined

问题五

(function f(f){
    return typeof f();//"number"
})(function(){ return 1; });

直接上注释解释:

(function f(f){
    //这里的f是传入的参数function(){ return 1; }
    //执行的结果自然是1
    return typeof f(); //所以根据问题一的表格我们知道,typeof 1结果是"number"
})(function(){ return 1; });

问题六

var foo = {
    bar: function() { return this.baz; },
    baz: 1
};

(function(){
    return typeof arguments[0]();//"undefined"
})(foo.bar);

这里你可能会误以为最终结果是number。向函数中传递参数可以看作是一种赋值,所以arguments[0]得到是是真正的bar函数的值,而不是foo.bar这个引用,那么自然this也就不会指向foo,而是window了。

问题七

var foo = {
    bar: function(){ return this.baz; },
    baz: 1
}
typeof (f = foo.bar)();//"undefined"

这和上一题是一样的问题,(f = foo.bar)返回的就是bar的值,而不是其引用,那么this也就指的不是foo了。

问题八

var f = (function f(){ return '1'; }, function g(){ return 2; })();
typeof f;//"number"

逗号操作符 对它的每个操作对象求值(从左至右),然后返回最后一个操作对象的值

所以(function f(){ return '1'; }, function g(){ return 2; })的返回值就是函数g,然后执行她,那么结果是2;最后再typeof 2,根据问题一的表格,结果自然是number

问题九

var x = 1;
if (function f(){}) {
    x += typeof f;
}
x;//"1undefined"

这个问题的关键点,我们在问题二中谈到过,function expression中的函数名f是不能在函数体外部访问的

问题十

var x = [typeof x, typeof y][1];
typeof typeof x;//"string"
  1. 因为没有声明过变量y,所以typeof y返回"undefined"

  2. typeof y的结果赋值给x,也就是说x现在是"undefined"

  3. 然后typeof x当然是"string"

  4. 最后typeof "string"的结果自然还是"string"

问题十一

(function(foo){
    return typeof foo.bar;//"undefined"
})({ foo: { bar: 1 } });

这是个纯粹的视觉诡计,上注释

(function(foo){

    //这里的foo,是{ foo: { bar: 1 } },并没有bar属性哦。
    //bar属性是在foo.foo下面
    //所以这里结果是"undefined"
    return typeof foo.bar;
})({ foo: { bar: 1 } });

问题十二

(function f(){
    function f(){ return 1; }
    return f();//2
    function f(){ return 2; }
})();

通过function declaration声明的函数甚至可以在声明之前使用,这种特性我们称之为hoisting。于是上述代码其实是这样被运行环境解释的:

(function f(){
    function f(){ return 1; }
    function f(){ return 2; }
    return f();
})();

问题十三

function f(){ return f; }
new f() instanceof f;//false

当代码new f()执行时,下面事情将会发生:

  1. 一个新对象被创建。它继承自f.prototype

  2. 构造函数f被执行。执行的时候,相应的传参会被传入,同时上下文(this)会被指定为这个新实例。new f等同于new f(),只能用在不传递任何参数的情况。

  3. 如果构造函数返回了一个“对象”,那么这个对象会取代整个new出来的结果。如果构造函数没有返回对象,那么new出来的结果为步骤1创建的对象,

ps:一般情况下构造函数不返回任何值,不过用户如果想覆盖这个返回值,可以自己选择返回一个普通对象来覆盖。当然,返回数组也会覆盖,因为数组也是对象。

于是,我们这里的new f()返回的仍然是函数f本身,而并非他的实例

问题十四

with (function(x, undefined){}) length;//2

with语句将某个对象添加的作用域链的顶部,如果在statement中有某個未使用命名空间的变量,跟作用域链中的某個属性同名,則這個变量将指向這個属性值。如果沒有同名的属性,则将拋出ReferenceError异常。

OK,现在我们来看,由于function(x, undefined){}是一个匿名函数表达式,是函数,就会有length属性,指的就是函数的参数个数。所以最终结果就是2

写在最后

有人觉得这些题坑爹,也有人觉得开阔了眼界,见仁见智吧。但有一件事是真的,无论你是否坚定的实践派,缺了理论基础,也铁定走不远 - 你永远不会见到哪个熟练的技术工人突然成了火箭专家。

看文档、读标准、结合实践,才是同志们的决胜之道。

以上就是关于14 个折磨人的 JavaScript 面试题的代码示例详细分析的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn