The front-end classic interview questions are not classic. Do not use star !, Front-end star

Source: Internet
Author: User

The front-end classic interview questions are not classic. Do not use star !, Front-end star
Preface

(The following content is described by a friend.) Today I want to share with you several front-end classic interview questions. Why did I suddenly want to write such an article? Today, at the request of the company, I went to the interview for a few recruiters, and then I searched for the front-end abnormal interview questions! HAHA, if I am not an abnormal, deceiving interviewer, I just want to see the logic of each other!

Then I used these interview questions for self-detection and found that there were still some pitfalls ~~~
Next, I would like to share with you the analysis of several questions!

Body

First, I will pick a few of them, which are not necessarily the most classic. But they will make you learn and make progress! List

// Question 1st ["1", "2", "3"]. map (parseInt) // question 2nd [[, 2, 1]. reduce (Math. pow), []. reduce (Math. pow)] // 3rd question var ary = [, 2]; ary [10] = 10; ary. filter (function (x) {return x = undefined;}); // question 4th [typeof null, null instanceof Object] // question 5th function sidEffecting (ary) {ary [0] = ary [2];} function bar (a, B, c) {c = 10 sidEffecting (arguments); return a + B + c ;} bar (, 1) // The sixth question var END = Math. pow (2, 53); var START = END-100; var count = 0; for (var I = START; I <= END; I ++) {count ++;} console. log (count );
Reader's thinking time

Think hard and work hard! ========================================================== ======

Next, let's analyze it slowly.

Question 1:

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

Knowledge points of this question include:

Follow the knowledge points above to list this question!

First, map accepts two parameters, One callback function callback, And the other callback function's this value. The callback function accepts three parameters: currentValue, index, and arrary, map only passes in the callback function -- parseInt. second, parseInt only accepts two parameters: string, radix (base ). this question is interpreted as the key and index, so this question is to ask parseInt ('1', 0); parseInt ('2', 1); parseInt ('3', 2 ); the first and second parameters are invalid. so the answer is [1, NaN, NaN] If the study understands parseInt (3, 8) parseInt (3, 2) // comment on the answer below to this question. Don't cheat! ParseInt (3, 0)

Question 2:

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

Knowledge points of this question:

  • Array/Reduce

This question is interspersed with knowledge points!

Arr. reduce (callback [, initialValue]) reduce accepts two parameters, One callback and one initial value. the callback function accepts the four parameters previusvalue, currentValue, currentIndex, and array. Note that If the array is empty and no initialValue was provided, TypeError wocould be thrown. so the second expression will report an exception. the first expression is equivalent to Math. pow (3, 2) => 9; Math. pow (9, 1) => 9

Answeran error

Question 3:

Var ary = [0, 1, 2]; ary [10] = 10; ary. filter (function (x) {return x = undefined;}); answer: []

Knowledge points:

  • Sparse array

Let's take a look at the polyfill of Array. prototype. filter:

If (! Array. prototype. filter) {Array. prototype. filter = function (fun/*, thisArg */) {'use strict '; if (this = void 0 | this = null) {throw new TypeError ();} var t = Object (this); var len = t. length >>> 0; if (typeof fun! = 'Function') {throw new TypeError ();} var res = []; var thisArg = arguments. length> = 2? Arguments [1]: void 0; for (var I = 0; I <len; I ++) {if (I in t) {// pay attention to this !!! Var val = t [I]; if (fun. call (thisArg, val, I, t) {res. push (val) ;}} return res ;};}

We can see that when the array is iterated, we first check whether the index value is an attribute of the array. Then we can test it.

0 in ary; => true3 in ary; => false10 in ary; => true

That is to say, from 3-9 is not initialized.bug !, These indexes do not exist in arrays. these pitfalls will be skipped when array functions are called.

Question 4:

[typeof null, null instanceof Object]

Knowledge point:

Typeof returns a string of the type.

The instanceof operator is used to check whether constructor. prototype exists in the prototype chain of the parameter object.

For this question, you can directly look at the link... because typeof null = 'object' is like this at the beginning of the language ....

For the result of typeof, see the following table:

type         resultUndefined   "undefined"Null        "object"Boolean     "boolean"Number      "number"String      "string"Symbol      "symbol"Host object Implementation-dependentFunction    "function"Object      "object"

Therefore, the answer is [object, false].

Question 5:

function sidEffecting(ary) {  ary[0] = ary[2]; }function bar(a,b,c) {   c = 10   sidEffecting(arguments);   return a + b + c;}bar(1,1,1)

Knowledge point:

  • Functions/arguments

First, The arguments object is an Array-like object corresponding to the arguments passed to a function.

That is to say, arguments is an object, and c is arguments [2]. Therefore, the modification to c is the modification to arguments [2.

So the answer is 21.

But !!!!

When the function parameter involves any rest parameters, any default parameters or any destructured parameters, this arguments is not a mapped arguments object .....

See:

function sidEffecting(ary) {  ary[0] = ary[2];}function bar(a,b,c=3) {  c = 10  sidEffecting(arguments);  return a + b + c;}bar(1,1,1)

The answer is 12 !!!!

Please feel it slowly !!

Question 6:

Cough and cough!
Careful friends found me6Question is not6The question isSat.Question
In fact, this is a question for you to think about. If you have any questions or discussions, please leave a message!

[I have a QQ Group for front-end learning and communication: 328058344 if you encounter any problems during the process of learning the front-end, please come to my QQ Group to ask questions. The group will update some learning resources every day. Chat is prohibited .]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.