Output result question

Keywords: Javascript node.js html

const obj = { 1: "a", 2: "b", 3: "c" };
const set = new Set([1, 2, 3, 4, 5]);

obj.hasOwnProperty("1");
obj.hasOwnProperty(1);
set.has("1");
set.has(1);

A: false true false true
B: false true true true
C: true true false true
D: true true true true

Answer: C

All object keys (excluding Symbols) will be stored as strings, even if you don't have a key of a given string type. This is why obj.hasOwnProperty ('1 ') also returns true.

The above statement does not apply to Set. There is no "1" in our Set: set.has ('1 ') returns false. It has numeric type 1, and Set. Has (1) returns true.

const a = {};
const b = { key: "b" };
const c = { key: "c" };

a[b] = 123;
a[c] = 456;

console.log(a[b]);

A: 123
B: 456
C: undefined
D: ReferenceError

Answer: B

Object keys are automatically converted to strings. We tried to set an object as the key of object a, with a value of 123.

However, when an object is automatically converted to a string, it becomes * * [object] * *. So what we're talking about here is a ["object"] = 123. Then we can try to do the same thing again. c objects also undergo implicit type conversion. Then, a ["object"] = 456.

Then we print a[b], which is actually a ["object"]. We set it to 456, so we return 456.

  1. Variable promotion of var
       var a = 10;                       //
        (function() {
            console.log(a);              //  undefined
            a = 5;                       //
            console.log(window.a);       // 10
            var a = 20;                  //  It is equivalent to declaring var a = undefined at the beginning
            console.log(a);              //20
        })()

  1. this of the arrow function points to where it is defined
const shape = {
  radius: 10,
  diameter() {
    return this.radius * 2;
  },
  perimeter: () => 2 * Math.PI * this.radius
};

shape.diameter();
shape.perimeter();

A: 20 and 62.83185307179586
B: 20 and NaN
C: 20 and 63
D: NaN and 63

Answer: B
Note that diameter is a normal function and perimeter is an arrow function.

For the arrow function, the this keyword points to the environment of its context (location at the time of definition), which is different from ordinary functions! This means that when we call perimeter, it does not point to the shape object, but to the environment (window) when it is defined. No value radius attribute, return undefined.

  1. Implicit conversion
+true;
!"Lydia";

A: 1 and false
B: false and NaN
C: false and false

Answer: A

The unary plus sign attempts to convert a boolean type to a numeric type. true is converted to 1 and false to 0.
The string 'Lydia' is a true value. What we are actually asking is "is this true value false?". This returns false.

	var a = 1;
    function Fn1(){
        var a = 2;
        console.log(this.a + a);
    }
    function Fn2(){
        var a = 10;
        Fn1();
    }
    Fn2();               

    var Fn3 = function(){
        this.a = 3;
    }
    Fn3.prototype = {
        a:4
    }
    var fn3 = new Fn3();
    Fn1.call(fn3);

3
5
When calling Fn2(), call Fn1 again. At this time, this points to window, this.a = 1, a=2.
fn3.Fn1(). This points to FN3, this.a=3, a=2

	var obj = {"key":"1","value":"2"};
    var newObj = obj;
    newObj.value += obj.key;
    console.log(obj.value);

21
Object shared address, one change, all change.
The + of the string is concatenation

	var m = 1, j=k=0;
    function add(n){
        return n = n+1;
    }
    y = add(m);
    function add(n){
        return n = n + 3;
    }
    z=add(m);
    console.log(y + ',' + z);

4,4
The last function with the same name shall prevail.

	function create(){
        let b = 100;
        return function(){
            console.log(b);
        } 
    }
    let fn = create();
    let b = 200;
    fn();

100
Closure, look up layer by layer

	function print(fn1){
        let d = 200;
        fn1();
    }
    let d = 100;
    function fn1(){
        console.log(d);
    }
    print(fn1);

100
When looking for variables in closures, start from the place defined by the function and look up!

let i
for(i = 0; i <=3; i++){
	setTimeout(function(){
		console.log(i)
	}, 0)
}

4444

for(let i = 0; i <=3; i++){
	setTimeout(function(){
		console.log(i)
	}, 0)
}

0123

Pay attention to the position of le declaration! It's inside.

let a = 100
function test(){
	alert(a)
	a = 10
	alert(a)
}
test()
alert(a)

100
10
10
alert is the method of window.

Posted by Maeltar on Thu, 23 Sep 2021 03:05:17 -0700