Refer: https://www.cnblogs.com/Answer1215/p/15084496.html
A string is a primitive value, and all primitive values are immutable.
Q1:
const a = "Frontend Masters"
let b = "Frontend Masters"
const c = { learnAt: "Frontend Masters" }
let d = { learnAt: "Frontend Masters" }
const e = Object.freeze({ learnAt: "Frontend Masters" })
.
.
.
.
.
.
Answer:
All primitive values are immutable, therefore a & b are both hold immutable values.
Repeat "All primitive values are immutable" three times...
All primitive values are immutable
All primitive values are immutable
All primitive values are immutable
Const and Let differ in terms of whether variables can be reassigned, but that has nothing to do with whether the values they hold can be modified.
Therefore both c & d does NOT hod ummutable values
Object.freeze prevents properties of an object from being changed, and prevents new properties from being added. This effectively is a "Shallow immutability". Which means Object.freeze won't help you mutate a nested object.
const e = Object.freeze({ course: 'Typescript', learnAt: {place: "Frontend Masters"} })
e.course = "Reacct" // report error
e.learnAt.place = "Udmey" // works
Q2:
const str = "hello"
let val =
/* ??? */
console.log(val)
/**
* {
* '0': 'h',
* '1': 'e',
* '2': 'l',
* '3': 'l',
* '4': 'o'
* }
*/
.
.
.
.
.
.
Answer:
let val = { ...str.split("") }
console.log(val)
/**
* {
* '0': 'h',
* '1': 'e',
* '2': 'l',
* '3': 'l',
* '4': 'o'
* }
*/
Q3:
Look at the types of
first
andsecond
below, as well as the compile error messages. What does your mental model tell you about howstring
andString
are different?let first: string & number
let second: String & Number
first = "abc"
Type 'string' is not assignable to type 'never'.
second = "abc"Type 'string' is not assignable to type 'String & Number'. Type 'string' is not assignable to type 'Number'.
second = new String("abc")Type 'String' is not assignable to type 'String & Number'. Type 'String' is missing the following properties from type 'Number': toFixed, toExponential, toPrecision
for the first: both string and number are primtive types, they are not such a value is both string and number at the same time, so that it results in a `never`.
for the second: String and Number are interface types. The Union of String and Number are common methods and props of both, but it won't results in a never type.
Q4:
In what order will the animal names below be printed to the console?
function getData() {
console.log("elephant")
const p = new Promise((resolve) => {
console.log("giraffe")
resolve("lion")
console.log("zebra")
})
console.log("koala")
return p
}
async function main() {
console.log("cat")
const result = await getData()
console.log(result)
}
console.log("dog")
main().then(() => {
console.log("moose")
})
.
.
.
.
.
.
.
Answer:
"dog" will come first
Then main() function will be invoked, so "cat" will come second.
Then getData() function will be invoked, so "elephant" will come third.
new Promise() constructor function will be invoked, 4th. "giraffe"
resolve() function will be invoked by the time we call .then()
so "zebra" comes 5th.
then "koala" comes 6th.
after await getData(), console.log(result) will run, so "lion" 7th.
"moose" will be last
- Are you surprised that
giraffe
andzebra
happen so early? Remember thatPromise
executors are invoked synchronously in thePromise
constructor - Are you surprised that
lion
happens so late? Remember that aresolve
is not areturn
. Just because aPromise
has resolved, doesn’t mean the corresponding.then
(orawait
is called immediately)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。