This is the 16th day of my participation in the August More Text Challenge
Let’s do it. Let’s do it
The continue problem
for (let i = 1; i < 5; i++) {
if (i === 3) continue;
console.log(i);
}
Copy the code
- A:
1
2
- B:
1
2
3
- C:
1
2
4
- D:
1
3
4
Answer: C
In this for loop, continue skips the iteration when I is equal to 3.
Constructor problem
String.prototype.giveMyFood = () = > {
return "Please give weirui food!";
};
const name = "weirui";
name.giveMyFood();
Copy the code
- A:
"Please give weirui food!"
- B:
TypeError: not a function
- C:
SyntaxError
- D:
undefined
Answer: A,
String is a built-in constructor to which we can add attributes. Add a method to its prototype here. A string of primitive type is automatically converted to a string object, which is generated by the string prototype function. Therefore, all strings (string objects) have access to this method!
When giveMyFood is called with a string of primitive type, the following actually happens:
- To create a
String
Is an instance of a wrapper type - Called on an instance
substring
methods - Destroy instance
Type conversion
const a = {};
const b = { key: "b" };
const c = { key: "c" };
a[b] = 123;
a[c] = 456;
console.log(a[b]);
Copy the code
- A:
123
- B:
456
- C:
undefined
- D:
ReferenceError
A [“Object Object “] = 123 when the Object is automatically converted to a string, it becomes [Object Object]. C objects are also implicitly cast. So, a[“Object Object “] = 456. At this point we print a[b], which is actually a[“Object Object “], so return 456.
Call the bind problem
const person = { name: "weirui" };
function sayHi(age) {
console.log(`The ${this.name} is ${age}`);
}
sayHi.call(person, 24);
sayHi.bind(person, 24);
Copy the code
- A:
undefined is 21
Lydia is 21
- B:
function
function
- C:
Lydia is 21
Lydia is 21
- D:
Lydia is 21
function
Answer: D
We pass the object referenced by this keyword, but the.call method executes immediately,
The.bind method returns a copy of the value of the function, but with the context of the binding, it is not executed immediately.
Type judgment
function sayHi() {
return (() = > 0) (); }typeof sayHi();
Copy the code
- A:
"object"
- B:
"number"
- C:
"function"
- D:
"undefined"
Answer: B
The sayHi function returns the return value of the function immediately called. This function returns 0 and is of type number.
JavaScript has seven built-in types: NULL, undefined, Boolean, number, String, Object, and symbol. Function is not a type because a function is an object and its type is Object.
Another type judgment problem:
Which values are false?
0;
new Number(0);
("");
("");
new Boolean(false);
undefined;
Copy the code
- A:
0
.' '
.undefined
- B:
0
.new Number(0)
.' '
.new Boolean(false)
.undefined
- C:
0
.' '
.new Boolean(false)
.undefined
- D: All are false values
Answer: A,
There are only six false values in JavaScript:
undefined
null
NaN
0
' '
(empty string)false
Function constructors such as new Number and New Boolean are true values.