This is the 19th day of my participation in the August More Text Challenge.
Execute functions and scopes immediately
var a = 123;
(function a () {
a = 'abc';
console.log(a); }) ();Copy the code
The answer:
ƒ a () {a ='abc';
console.log(a);
}
Copy the code
instructions
- Executing the function IIFE immediately simulates block-level scope, declaring variables inside a function expression and then calling the function immediately. Variables in the function body scope are as if they were in the block-level scope.
- If the immediate function name is the same as the internal variable name, the function name takes precedence
- In strict mode ‘use strict’; The following code will report an error
The String and the switch
var str = new String(123);
switch (str) {
case '123': {
console.log('string 123');
break;
}
case 123: {
console.log(`number 123`);
break;
}
case undefined: {
console.log(`undefined value`);
break;
}
default: {
console.log('unknown value');
break; }}Copy the code
Answer: Unknown value is displayed
instructions
-
- var str = new String(123); STR is an example of the String class of type Object
-
- Switch is strictly comparative, so STR === ‘123’ is false
-
- STR does not match any of the preceding values. Run the default command to output unknown value
-
- MDN mentions that a switch statement first evaluates its expression. It then starts from the first case clause until it finds a clause whose expression value is equal to the value of the input expression (usingStrict operators (EN-US).
= = =
) and transfers control to the clause to execute the statement.
- MDN mentions that a switch statement first evaluates its expression. It then starts from the first case clause until it finds a clause whose expression value is equal to the value of the input expression (usingStrict operators (EN-US).
Object. GetPrototypeOf prototype
function A() {};
var a0 = new A();
var a1= {};
a1.__proto__ = A.prototype;
var a2 = {};
a2.__proto__ = Object.getPrototypeOf(new A());
var a3 = {};
a3.__proto__ = Object.getPrototypeOf(A);
console.log(a0 instanceof A);
console.log(a1 instanceof A);
console.log(a2 instanceof A);
console.log(a3 instanceof A);
Copy the code
Answer :true, true, true, false
instructions
The object.getProtoTypeof () method returns the Prototype of the specified Object (the value of the internal [[Prototype]] property)
var a0 = new A();
// the a0 prototype __proto__ automatically points to a.prototype
// so a0 instanceof A is true;
var a1= {};
a1.__proto__ = A.prototype;
// a1 prototype __proto__ points directly to a.prototype
// so a0 instanceof A is true;
var a2 = {};
a2.__proto__ = Object.getPrototypeOf(new A());
// object.getProtoTypeof (new A()) is A prototype pointing to A new A() Object.
// So a2.__proto__ points to a.prototype
// so a2 instanceof A is true;
var a3 = {};
a3.__proto__ = Object.getPrototypeOf(A);
// object.getProtoTypeof (A) is A prototype pointing to A function.
// so a2.__proto__ points to function.prototype
// so a2 instanceof A is false;
Copy the code