May 31, 2021 Article blog
2. Q2: Which of the following array-to-object methods is more appropriate?
4. Q4: Is Object.keys equal to Object.values?
5. Q5: Basic recursive inspection?
6. Question 6: Functions are equal
7. Question 7: Object properties are equal
8. Question 8: Function function syntax
The article comes from the public number: front-ender
The following JavaScript questions are designed to be challenging and instructive. If you know exactly how to answer every question, that's fine, but if you've made some mistakes and know why, I think it's better!
Do the following snippets call function expressions (IIFE), higher-order functions (HOFs), or neither?
((fn, val) => {
return fn(val);
})(console.log, 5);
5
const arr = [1, 2, 3];
//第一种
const a = arr.reduce(
(acc, el, i) => ({ ...acc, [el]: i }),
{}
);
//第二种
const b = {};
for (let i = 0; i < arr.length; i++) {
b[arr[i]] = i;
}
第二种
When the b-object is set,
b[arr[i]]
property is set to the current index for each iteration. W
hen setting a, extend the syntax (...) A
cc creates a shallow copy of the accumulator object () at each iteration and sets a new property. T
his shallow copy is more wasteful than not performing a shallow copy. a
You need to construct two intermediate objects before the result is reached, and b does not construct any intermediate objects.
As a result, b is set more efficiently.
Consider the following function: the functionality of
superheroMaker
What is output when we pass the following two parameters?
const superheroMaker = a => {
return a instanceof Function ? a() : a;
};
console.log(superheroMaker(() => 'Batman'));
console.log(superheroMaker('Superman'));
"Batman" "Superman"
When passing
() => 'Batman'
to
superheroMaker
a
is the instance
Function
T
herefore, the function is called and the string
"Batman"
W
hen
"Superman"
to
superheroMaker
a it is not
a
Function
so
"Superman"
only strings.
Therefore, the outputs are "Batman" and "Superman".
Object Keys, Object Values
const obj = {
1: 1,
2: 2,
3: 3
};
console.log(Object.keys(obj) == Object.values(obj));
false
In this case,
Object.keys
the key to the string
["1", "2", "3"]
but
Object.values
returns:
[1, 2, 3]
Even if the values are of the same type, they are not the same object, so equal comparisons return
false
.
Consider the following recursive functions. If you pass the string "Hello World" to it, what is the output?
const myFunc = str => {
if (str.length > 1) {
return myFunc(str.slice(1));
}
return str;
};
console.log(myFunc('Hello world'));
"d"
The first time the function is called, str.length is greater than 1 ("Hello World" is 11 characters), so we return the same function called, str.slice(1), which is string "ello World". W e repeat this procedure until the string is only one character long: the character "d", which returns to the initial call to myFunc. Then, we record the character.
What does the following code output?
const a = c => c;
const b = c => c;
console.log(a == b);
console.log(a(7) === b(7));
false,true
In the first test, a and b are different objects in memory; T herefore, a is not equal to b. I n the second test, a (7) returns the number 7 and b (7) returns number 7. These original types are strictly equal to each other.
In this case, it doesn't matter what the equal (-) and identity-comparison operators are;
a
and
b
have the same different object
firstName
properties.
Are these properties strictly equal to each other?
const a = {
firstName: 'Bill'
};
const b = {
firstName: 'Bill'
};
console.log(a.firstName === b.firstName);
true
The answer is yes. a .firstName is the string value "Bill" and b.firstName is the string value "Bill". Two identical strings are always equal.
Suppose myFunc is a function, val1 is a variable, and val2 is a variable. Does JavaScript allow the following syntax?
myFunc(val1)(val2);
允许
This is a common pattern of higher-order functions. I
f
myFunc(val1)
returns a function, the function calls
val2
as an argument.
This is a practical example that you can try:
const timesTable = num1 => {
return num2 => {
return num1 * num2;
};
};
console.log(timesTable(4)(5));
// 20
const a = { firstName: 'Joe' };
const b = a;
b.firstName = 'Pete';
console.log(a);
{ firstName: 'Pete' }
When we set up in the second line, b and a point to the same object in memory. Therefore, firstName, b Changing the property to on will change the property of the unique object in firstName memory, so a.firstName will reflect this change.
Will the following function always return the maximum number in the array?
function greatestNumberInArray(arr) {
let greatest = 0;
for (let i = 0; i < arr.length; i++) {
if (greatest < arr[i]) {
greatest = arr[i];
}
}
return greatest;
}
不是
If there is a value greater than 0 in the array, it is correct, and if they are all less than 0, it returns
0
This function is correct for an array with at least one value of 0 greater than or equal to one.
However, if all numbers are below, it returns
0
That's what
W3Cschool编程狮
has
learned about 10 challenging JavaScript quiz questions
and answers.