146. What is the output?
function getFruit(fruits) { console.log(fruits?.[1]?.[1]) } getFruit([['🍊', '🍌'], ['🍍']]) getFruit() getFruit([['🍍'], ['🍊', '🍌']])
Answer: D. this question mainly focuses on the optional chain operator. As long as it does not exist, it returns undefined, so choose D.
147. What is the output?
class Calc { constructor() { this.count = 0 } increase() { this.count ++ } } const calc = new Calc() new Calc().increase() console.log(calc.count)
A: a. the object calling increase and calc are not the same object, so the answer is 0
148. What is the output?
const user = { email: "e@mail.com", password: "12345" } const updateUser = ({ email, password }) => { if (email) { Object.assign(user, { email }) } if (password) { user.password = password } return user } const updatedUser = updateUser({ email: "new@email.com" }) console.log(updatedUser === user)
A: B. in this question, the address of the user object is not modified, but the internal address is modified, so the updatedUser and user addresses are the same.
149. Output what?
const fruit = ['🍌', '🍊', '🍎'] fruit.slice(0, 1) fruit.splice(0, 1) fruit.unshift('🍇') console.log(fruit)
A: C, this question is to check our familiarity with common API s. splice will have side effects on the original array, and unshift adds elements to the front of the array, so the answer is C.
150. What is the output?
const animals = {}; let dog = { emoji: '🐶' } let cat = { emoji: '🐈' } animals[dog] = { ...dog, name: "Mara" } animals[cat] = { ...cat, name: "Sara" } console.log(animals[dog])
A: B. an object is used as the key, and the underlying storage is "object Object". Therefore, the keys of dog and cat in animals are the same, so the corresponding value of cat can override dog, so choose B.
151. What is the output?
const user = { email: "my@email.com", updateEmail: email => { this.email = email } } user.updateEmail("new@email.com") console.log(user.email)
Answer: A. this question examines the problem pointed by the arrow function this. At this time, this points to the global object. Because there is no email attribute in the global, an error will be reported:
152. What is the output?
const promise1 = Promise.resolve('First') const promise2 = Promise.resolve('Second') const promise3 = Promise.reject('Third') const promise4 = Promise.resolve('Fourth') const runPromises = async () => { const res1 = await Promise.all([promise1, promise2]) const res2 = await Promise.all([promise3, promise4]) return [res1, res2] } runPromises() .then(res => console.log(res)) .catch(err => console.log(err))
A: D, this question mainly examines our understanding of promise All knows that as long as a reject is received, the asynchronous result is directly returned to the callback function that captures the error, so print Third
153. Which method value can print {name: "Lydia", age: 22}?
const keys = ["name", "age"] const values = ["Lydia", 22] const method = /* ?? */ Object[method](keys.map((_, i) => { return [keys[i], values[i]] })) // { name: "Lydia", age: 22 }
A: C, this question is mainly to check whether we know how to convert a two-dimensional array into an object. You can use the API C,
154. What is the output?
const createMember = ({ email, address = {}}) => { const validEmail = /.+\@.+\..+/.test(email) if (!validEmail) throw new Error("Valid email pls") return { email, address: address ? address : null } } const member = createMember({ email: "my@email.com" }) console.log(member)
Answer: C, one thing we need to know about this question is that the empty object belongs to the true value, so when returning the object, the address does not return null, but {}
155. What is the output?
let randomValue = { name: "Lydia" } randomValue = 23 if (!typeof randomValue === "string") { console.log("It's not a string!") } else { console.log("Yay it's a string!") }
A: B, first of all, we need to know the operation priority of this problem. First, we will calculate the typeof randomValue. This time, we will return a "number", which is a string. Take the inverse of the string and return false. False is not equal to "string", so the last output is the statement corresponding to else, "Yay it's a string!".
Topic source
https://github.com/lydiahallie/javascript-questions