文章目錄
|
|
reaction 會是什麼?
答案: yikes
Primitive Values Are Immutable
Strings (which are primitive) and arrays (which are not — they’re objects!) have some superficial similarities. An array is a sequence of items, and a string is a sequence of characters:
|
|
You can access the first array item similarly to how you would access a string’s first character. almost feels like strings are arrays (but they’re not!):
|
|
You can change an array’s first item:
|
|
So intuitively, it’s easy to assume that you can do the same to a string:
|
|
But you can’t.
|
|
Like any number, 50 is a primitive value, and you can’t set properties on it.
all primitive values exist in the outer circle further from my code — like distant stars. This reminds me that even though I can refer to them from my code, I can’t change them. They stay what they are.
Variables Are Wires
|
|
Assigning a Value to a Variable
|
|
Variables are not values. Variables point to values.
|
|
Expressions always result in a single value
|
|
- The left side of an assignment must be a “wire”
- The right side of an assignment must be an expression.
If the right side must be an expression, does this mean that numbers like
2
or strings like'The Kraken'
written in code are also expressions? Yes! Such expressions are called literals — because we literally write down their values.
不用浪費時間糾結 “pass a variable” or “pass a value”
Who cares if you say “pass a variable” or “pass a value”? Isn’t the difference hopelessly pedantic? I certainly don’t encourage interrupting your colleagues to correct them — or even yourself. That would be a waste of everyone’s time.
很單純:you can’t pass a variable — at least, not in JavaScript.
you can’t pass a variable_範例1
|
|
If we thought double(money) was passing a variable, we could expect that x = x * 2 would double that variable. But that’s not how it works. We know that double(money)
means “figure out the value of money
, and then pass that value to double
”. So the answer is 10
. What a scam!
you can’t pass a variable_範例2
|
|
y = x
did not mean point y to x.
x = 0
We can’t point variables to each other! Variables always point to values.
變數賦值:使用『線段』指向的概念;而不是用『箱子』裝東西的概念
Why can’t we just “put
0
and10
values into the variables rather than pointing variables to them?
Using wires is going to be very important for explaining numerous other concepts, like strict equality, object identity, and mutation. We’re going to stick with wires, so you might as well start getting used to them now! My universe is full of wires.
Recap
- Primitive values are immutable.
- Variables are not values. Each variable points to a particular value.(額外補充:變數沒有型別,『值』才有型別)
- Variables point to values. (We can’t point variables to each other! Variables always point at values.)