Do you really know JavaScript

JavaScript is a strange language and not really easy to master. Without further ado, let’s take a quick test, five questions, to see if you really understand JavaScript. Are you ready? To begin 😄

The title

No.1

if(! ("a" in window)) {
    var a = 1;
}

console.log(a);Copy the code

No.2

var a = 1,
    b = function a(x) {
        x && a(--x);
    };
console.log(a);Copy the code

No.3

function a(x) {
    return x * 2;
}
var a;
console.log(a);Copy the code

No.4

function b(x, y, a) {
    arguments[2] = 10;
    console.log(a);
}
b(1.2.3);Copy the code

No.5

function a() {
    console.log(this);
}
a.call(null);Copy the code

parsing

Topics from 👉 dmitry.baranovskiy.com/post/914032… Analysis with their own understanding 😄

No.1

In the browser environment, a global variable is an attribute of window, i.e. var a = 1 is equivalent to window.a = 1. The in operator is used to determine whether a property belongs to an object, either directly or as a property inherited from Prototype. In the browser, if there is no global variable a, declare a global variable a (ES5 has no block-level scope) and assign a value of 1. A lot of people would think it’s a 1. No, don’t forget that variable declarations are preloaded! What does that mean? So this is the same thing as

var a;

if(! ("a" in window)) {
    a = 1;
}

console.log(a);Copy the code

So the variable a is already declared, but the if statement is preceded by undefined, so the if statement is never executed at all. And the answer is undefined

No.2

There are a few caveats to this question:

  1. Variable declarations and function declarations are preceded, but function expressions are not. For example:
console.log('b', b); // b undefined
var b = function() {}
console.log('b', b); // b function () {}Copy the code

2. The name of a named function expression can only be called inside the function, for example (excluding old IE😂) :

var foo = function bar () {}

console.log('foo', foo); 
// foo function bar(){}

console.log('bar', bar);
// Uncaught ReferenceError: bar is not definedCopy the code

So if you put those two together, and you look at the problem, you’re going to get 1

No.3

Function declarations override variable declarations, but not variable assignments. For example:

function foo(){
    return 1;
}
var foo;
console.log(typeof foo);    // "function"Copy the code

The function declaration takes precedence over the variable declaration, but if the variable foo is assigned, the result is completely different:

function foo(){
    return 1;
}
var foo = 1;
console.log(typeof foo);    // "number"Copy the code

After the variable foo is assigned, the initialization of the variable assignment overrides the function declaration. So you have to pay attention to this and look at the problem again

function a(x) {
    return x * 2;
}
var a;
console.log(a); // function a(x) {... }Copy the code

No.4

Arguments are dynamically bound to function arguments (see 👉JavaScript), so the output of arguments is 10

But but, let’s not forget the special case of strict mode, where arguments are equivalent to a copy of function arguments and are not dynamically bound, for example:

'use strict'
// Strict mode!!

function b(x, y, a) {
    arguments[2] = 10;
    console.log(a);
}
b(1.2.3); / / 3Copy the code

No.5

function a() {
    console.log(this);
}
a.call(null);Copy the code

About the a.c all (null); According to the ECMAScript262 specification, the call method takes the global object (the window object in the browser) as this if the first argument is passed to the object caller as null or undefined. So, whenever you pass null or undefined, its this is the global object Window. So, on the browser the answer is to print the Window object.

But but still, we can’t forget a special case — strict mode, where null is null and undefined is undefined, for example:

'use strict';
// Strict mode!!

function a() {
    console.log(this);
}
a.call(null); // null
a.call(undefined); // undefinedCopy the code

remind

  1. The global object in the browser iswindowIs, the Node. Jsglobal;
  2. In order to make the code more rigorous and robust, it is recommended to write JS with strict mode'use strict';
  3. ES6 has become a front-end necessary skills, we call on everyone to use ES6, convenient and efficient, you can use Babel to convert ES6 into ES5 or even ES3, try to use ES6 to promote the development of the front-end 👍

About

GitHub: 👉github.com/microzz Personal website: 👉microzz.com/