Analysis of JavaScript interview

Source: Internet
Author: User

some of the questions in Javascript make a lot of students feel headache, the following is based on brother Lian Education

Graduate students interview face questions, to make a simple share, I hope that the first into the workplace you have some help:JavaScript face question analysis.

First question

/*

parsing:

+ priority greater than ?

This problem is equivalent to: ' Value is true '? ' Something ': ' Nothing '

so the result:' Something '

*/

var val = ' Smtg ';

Console.log (' Value is ' + (val = = = ' Smtg ')? ' Something ': ' Nothing ');

Second question

/*

* Analysis:

* typeof Returns a String representing the type

See below for typeof results :

**type** **result**

Undefined "Undefined"

Null "Object"

Boolean "Boolean"

Number "number"

Symbol "symbol"

Hostobject implementation-dependent

function "function"

Object "Object"

The instanceof operator is used to detect if the Constructor.prototype exists on the prototype chain of the Parameter object

So output ["Object", false]

*/

Functiontwo () {

Console.info ([Typeofnull, null instanceof Object]); ["Object", false]

Third question

/*

[ sparse arrays and dense arrays in JavaScript ][1]

parsing:

in general, the arrays in JavaScript are sparse , that is , there can be gaps between the elements in the array

In fact , there is no regular array in JavaScript, and all arrays are actually an object.

the array of JavaScript is not indexed at all, because the index is a number, and the index of the array in JS is string,

ARR[1] In fact is arr["1", to arr["" "] =1,arr.length will automatically become 1001.

The root cause of these manifestations is that the object in JavaScript is a key-value pair of strings to any value . Note The key can only be a string .

Take a look at some of the Array.prototype.filter code :

var t =object (this);

var len =t.length >>> 0;

if (typeoffun!== ' function ') {

Thrownew TypeError ();

}

var res =[];

Varthisarg = Arguments.length >= 2? ARGUMENTS[1]: void 0;

for (var i= 0; i < len; i++) {

if (i int) {// note here !!!

Varval = T[i];

if (Fun.call (Thisarg, Val, I, T)) {

Res.push (Val);

}

}

}

from the above , when the filter array is traversed, it first checks whether the index value is an attribute of the array . Test it:

Console.info (0 in ary); True

Console.info (1 in ary); True

Console.info (4 in ary); False

Console.info (ten in ary); False

which means that 3~9 's index is not initialized at all.

so the answer:[];

*/

var ary = [0,1,2];

ARY[10] = 10;

Console.info (Ary.filter (function (x) {return x ===undefined;}));

Question Fourth

/*

parsing:

Y is assigned to the global . x is a local variable . So when you print X, it's referenceerror .

*/

(function () {

var x = y = 1;

})();

Console.log (y); 1

Console.log (x); Error

Question Fifth

/*

parsing:

when the function parameter involves any rest parameters,

Anydefault parameters or any destructured parameters ,

This arguments is not a mapped arguments object ...

so the answer is 12, and this needs to be well understood.

*/

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);

Analysis of JavaScript interview

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.