Analysis of JavaScript interview

Source: Internet
Author: User

Javascript some of the questions to make many students feel headache, the following on the basis of Brother Lian Education ( www.lampbrother.net graduate students interview face questions, to make a simple share, I hope that the first into the workplace you have some help: parsing of JavaScript face questions .

Section a Questions

/*

Analytical:

+ 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

/*

Resolution

* typeof returns a String representing the type

See below for typeof results:

**type** **result**

Undefined "Undefined"

Null "Object"

Boolean "Boolean"

Number "number"

Symbol "symbol"

Host Object 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]

*/

function () {

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

Section three Questions

/*

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

Analytical:

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 (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) {//NOTE here!!!

var val = 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;}));

Section Four Questions

/*

Analytical:

Y is assigned a global value. X is a local variable. So when I print X, I referenceerror it.

*/

(function () {

var x = y = 1;

})();

Console.log (y); 1

Console.log (x); Error

Section Five Questions

/*

Analytical:

When the function parameter involves any rest parameters,

Any when the default 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.