Just JavaScript_03: Values and Variables

1
2
3
let reaction = 'yikes';
reaction[0] = 'l';
console.log(reaction);

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:

1
2
let arr = [212, 8, 506];
let str = 'hello';

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!):

1
2
console.log(arr[0]); // 212
console.log(str[0]); // "h"

You can change an array’s first item:

1
2
arr[0] = 420;
console.log(arr); // [420, 8, 506]

So intuitively, it’s easy to assume that you can do the same to a string:

1
str[0] = 'j'; // ???

But you can’t.

1
2
let fifty = 50;
fifty.shades = 'gray'; // No!

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

1
2
3
let pet = 'Narwhal';
pet = 'The Kraken';
console.log(pet); // "The Kraken"

Assigning a Value to a Variable

1
let pet = 'Narwhal';

Variables are not values. Variables point to values.

1
pet = 'The Kraken';

Expressions always result in a single value

1
pet = count + ' Dalmatians';
  • 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

1
2
3
4
5
6
7
function double(x) {
   x = x * 2;
}

let money = 10;
double(money);
console.log(money); // ?

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

1
2
3
4

let x = 10;
let y = x;
x = 0;

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 and 10 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.)

觀念小測驗

JJS: Values and Variables

最後更新 Mar 09, 2024 21:12 +0800