How many of these basic questions can you answer correctly? (test your basic mastery, with answer analysis [in])

Question 11:

var name = "erdong";
var object = {
    name: "chen",
    getNameFunc: function () {
        return function () {
            return this.name;
        }
    }
}
console.log(object.getNameFunc()());

Resolution:

// output

erdong
//   Copy code object.getNameFunc()(), execute first object.getNameFunc() returns a function:
function () {
    return this.name;
}
//   Copy the function returned by the code and execute it again, equivalent to
(function () {
    return this.name;
})();
//   Copy the code and bind this to window. Therefore, the value erdong of the global variable name is output.

Question 12

var name = "erdong";
var object = {
    name: "chen",
    getNameFunc: function () {
        var that = this;
        return function () {
            return that.name;
        }
    }
}
console.log(object.getNameFunc()());

Resolution:

//output
chen

object.getNameFunc() when executing, this in getnamefunc is bound to object, so that = object. object.getNameFunc() when the returned function is executed again, a closure is generated, so the returned function can also access the variable that in the outer scope, so object.name by object.name , namely chen.

Question 13

(function() {
  var a = b = 3;
})();
console.log(typeof a === 'undefined');
console.log(typeof b === 'undefined');

Resolution:

// output
true
false

First, understand how var a = b = 3 is executed. Pseudo code:

b = 3;
var a = b;

Therefore, when the self executing function is executed, b is a global variable because it is not declared by operators such as var. A is a local variable in the scope of the function. Therefore, when a and b are accessed outside, their values are ReferenceError: a is not defined and 3, respectively. However, typeof will not throw an error when detecting undeclared variables, and will return 'undefined'. So typeof a and typeof b return 'undefined' and 'number' respectively.

Question 14

var a = 6;
setTimeout(function () {
    a = 666;
}, 0)
console.log(a);

Resolution:

//Output
6

setTimeout is a macro task. Even if the delay is set to 0ms, it will not be executed until the synchronization code is executed. therefore console.log(a) Output 6

Question 15

function fn1() {
    var a = 2;
    function fn2 () {
      a++;
      console.log(a);
    }
    return fn2;
}
var f = fn1();
f();
f();

Resolution:

//Output
3
4

Because the fn1 function returns function fn2 after execution, a closure is generated. Therefore, a in fn2 accesses the variable a in the scope of fn1, so a + +, a is 3 after the first time, and a is 4 after the second time.

Question 16

var a = (function(foo){
    return typeof foo.bar;
})({foo:{bar:1}});

console.log(a);

Resolution:

//output

undefined

The value of the actual parameter foo is {foo:{bar:1 }, so typeof foo.bar Is undefined.
typeof foo.foo.bar Is number.

Question 17

function f(){
    return f;
}
console.log(new f() instanceof f);

Resolution:

//output

false

Because the return value of constructor f is f. So the value of new f() is f. therefore console.log(new f() instanceof f) is console.log(f instanceof f), that is, false.

Question 18

function A () {
}
A.prototype.n = 1;

var b = new A();

A.prototype = {
    n: 2,
    m: 3
}
var c = new A();

console.log(b.n, b.m);
console.log(c.n, c.m);

Resolution:

// output

1,undefined
2,3

var b = new A(); when instantiating b, the prototype of A is

A.prototype = {
    constructor:A,
    n:1
}

When accessing b.n and b.m, find the object pointed by A.prototype through the prototype chain, that is, b.n = 1,b.m = undefined.

var c = new A(); when instantiating c, the prototype of A is

A.prototype = {
    n: 2,
    m: 3
}

When accessing a.n and a.m, find the object pointed to by A.prototype through the prototype chain. At this time, A.prototype is rewritten, so a.n = 2,b.m = 3.

Question 19

var F = function(){};
var O = {};
Object.prototype.a = function(){
    console.log('a')
}
Function.prototype.b = function(){
    console.log('b')
}
var f = new F();

F.a();  
F.b();  
O.a();
O.b();  

Resolution:

// output
a
b
a
TypeError: O.b is not a function

F is a Function. It can also access methods on the Object prototype. O is an Object. It cannot access methods on the Function prototype.

The prototype chain of F is:

F => F.__proto__ => Function.prototype => Function.prototype.__proto__ => Object.prototype

because Object.prototype On the prototype chain of F, so f can access Object.prototype Properties and methods on. That is, F.a(),F.b() can be accessed normally.

The prototype chain of O is:

O => O.__proto__ => Object.prototype

because Function.prototype Not on O's prototype chain, so o cannot access Function.prototype The method on, O.b(), throws an error.

If you have a good grasp of prototypes and prototype chains, try to understand the following examples:

console.log(Object instanceof Function);

console.log(Function instanceof Object);

console.log(Function instanceof Function);

Question 20

function Person() {
    getAge = function () {
        console.log(10)
    }
    return this;
}

Person.getAge = function () {
    console.log(20)
}

Person.prototype.getAge = function () {
    console.log(30)
}

var getAge = function () {
    console.log(40)
}

function getAge() {
    console.log(50)
}


Person.getAge();
getAge();
Person().getAge();
new Person.getAge();
getAge();
new Person().getAge();

Resolution:

//Output

20
40
10
20
10
30

Person.getAge(); the getage method on the person function is executed at this time.

Person.getAge = function () {
    console.log(20)
}

So output: 20.
getAge(); the getAge method in the global is executed at this time. At this time, the global getAge method is:

function () {
    console.log(40)
}

So output: 40.

Person().getAge(); because person() is executed separately, this in the scope is bound to window, which is equivalent to window.getAge(). As above, the global getage method is executed, but when the person is executed, it is executed internally

getAge = function () {
    console.log(10)
}

So the global getAge method is now:

function () {
    console.log(10)
}

So output: 10.

new Person.getAge (); this is equivalent to instantiation Person.getAge This function, pseudo code:

var b = Person.getAge;
new b();
// So output: 20
getAge();Execute global getAge method,Because of the Person().getAge()Put the whole situation into practice getAge Method is assigned as:
function () {
    console.log(10)
}
// Copy code so output: 10.
new Person().getAge();This is called Person Archetypal getAge method:
Person.prototype.getAge = function () {
    console.log(30)
}
// Copy code so output: 30.
// Note here: 1. Variable promotion and assignment after promotion. 2. When calling the constructor, the difference between with () and without ().

Posted on Sun, 14 Jun 2020 00:21:10 -0400 by Mr Camouflage