Javascript interview questions analysis

Some interview questions of Javascript make many students feel a headache. The following is a simple sharing based on the interview questions encountered by graduates of Brothers Education (www.lampbrother.net), hoping to help you who are new to the workplace. : Analysis of Javascript interview questions.

The first question
/*
   Analysis:
     + Priority is greater than?
   This question is equivalent to: 'Value is true' ? 'Something' : 'Nothing'
   So the result is: 'Something'
*/
var val = 'smtg';
console.log ('Value is ' + (val === 'smtg') ? 'Something' : 'Nothing');

The second question
/*
* Analysis:
* typeof returns a string representing the type.
Please see the result of typeof below:
* *type** **result**
Undefined "undefined"
Null "object"
Boolean "boolean"
Number "number"


Function "function"
Object "object"

instanceof operator is used to check whether constructor.prototype exists on the prototype chain of parameter object,
            
so output ["object", false]
*/
function two(){
console.info([typeof null, null instanceof Object]); //["object", false]

The third question
/*
    [Sparse array and dense array in JavaScript][1]
   
    Analysis:
      Generally speaking, the array in JavaScript is sparse, that is to say , There can be gaps between the elements
      in the array In fact, there is no regular array in javascript, all arrays are actually an object.
      The array of javascript has no index at all, because the index is a number, and the index of the array in js is string,
      arr[1] is actually arr["1"], give arr["1000"] = 1, arr.length will also be automatically It becomes 1001.
      The root cause of these behaviors is that objects in JavaScript are key-value pairs from strings to arbitrary values. Note that keys can only be strings.
   
    Take a look at some of the code for Array.prototype.filter:
   

    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 here!!!
        var val = t[i];
        if (fun.call(thisArg, val, i, t)) {
          res.push(val);
        }
      }
    }
    As can be seen from the above, when filter traverses the array, it will first check 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(10 in ary); //false
    That is to say, the index of 3~9 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;}));

The fourth question
/*
    Analysis:
        y is assigned to the global. x is a local variable. So when printing x, a ReferenceError will be reported
   
*/
(function(){
  var x = y = 1;
})();
console.log(y); // 1
console.log(x); // error

Question 5
/*
    Analysis:
        When function parameters involve any rest parameters,
        any default parameters or any destructured parameters ,
        the arguments are no longer a mapped arguments object...,
        so the answer is 12, which 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);

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326646320&siteId=291194637