Just JavaScript_05:Counting the Values (Part 2)

BigInts

BigInts were only recently added to JavaScript, so you won’t see them used widely yet. If you use an older browser, they won’t work. Regular numbers can’t represent large integers with precision, so BigInts fill that gap (literally):

1
2
3
4
5
6
let alot = 9007199254740991n; // Notice n at the end
console.log(alot + 1n); // 9007199254740992n
console.log(alot + 2n); // 9007199254740993n
console.log(alot + 3n); // 9007199254740994n
console.log(alot + 4n); // 9007199254740995n
console.log(alot + 5n); // 9007199254740996n

String (primitive value)

Strings Aren’t Objects

Strings Aren’t Objects.All strings have a few built-in properties.

1
2
3
4
let cat = 'Cheshire';
console.log(cat.length); // 8
console.log(cat[0]); // "C"
console.log(cat[1]); // "h"

This doesn’t mean that strings are objects! String properties are special and don’t behave the way object properties do. For example, you can’t assign anything to cat[0]. Strings are primitives, and all primitives are immutable.

Object

1
2
3
4
5
console.log(typeof({})); // "object"
console.log(typeof([])); // "object"
console.log(typeof(new Date())); // "object"
console.log(typeof(/\d+/)); // "object"
console.log(typeof(Math)); // "object"

Unlike everything before, objects are not primitive values. This also means that by default, they’re mutable. We can access their properties with . or []

1
2
3
let rapper = { name: 'Malicious' };
rapper.name = 'Malice'; // Dot notation
rapper['name'] = 'No Malice'; // Bracket notation

Making Our Own Objects

In our mental model, all of the primitive values we’ve discussed — null, undefined, booleans, numbers, and strings — have “always existed”. We can’t “make” a new string or a new number, we can only “summon” that value:

1
2
let sisters = 3;
let musketeers = 3;

Every time we use the {} object literal, we create a brand new object value:

1
2
3

let shrek = {};
let donkey = {};

Do Objects Disappear?

You might wonder: do objects ever disappear, or do they hang around forever?

JavaScript is designed in a way that this is not directly observable from our code (although that might change).

You can’t destroy an object that you’ve created:

1
2
let junk = {};
junk = null; // Doesn't necessarily destroy an object

JavaScript is a garbage-collected language. Practically, this means that values might eventually disappear from my universe if I can’t reach them by following any wires from my code. But JavaScript doesn’t offer guarantees about when that happens.

In my universe, objects and functions float closest to my code. This reminds me that I can manipulate them and even make more of them.

Functions

Consider this example:

1
2
3
4
for (let i = 0; i < 7; i++) {
  let dwarf = {};
  console.log(dwarf);
}

How many objects did we create? We haven’t talked about scopes yet, so it’s fine if you can’t figure out a diagram for this snippet of code yet. The answer is that we create seven objects — one in each iteration of the loop. Now look at this example:

1
2
3
4
5
6
for (let i = 0; i < 7; i++) {
  let dig = function() {
    // Do nothing
  };
  console.log(dig);
}

How many functions do you see here? Is it one function, or is it seven?

The snippet above contains one function definition in the code, but it creates seven function values! This is why separating these concepts is important.

Every time we execute a line of code that contains a function declaration, a brand new function value appears in our universe.

This might remind you of how whenever we execute code like let dwarf = {}, a new object appears.

We create objects, and we create functions.

Recap

  • Counting the Values (Part 1)
  • Numbers: One value for each floating point math number.
  • BigInts: One value for every conceivable integer.
  • Strings: One value for every conceivable string.
  • Symbols: We skipped Symbols for now, but we’ll get to them someday!

The types below are special because they let us make our own values:

  • Objects: One value for every object literal we execute.
  • Function: One value for every function definition we execute.

writing 2 or “hello” always “summons” the same number or a string value.

But writing {} or declaring a function always creates a brand new, different value.

This idea is crucial to understanding equality in JavaScript.

觀念小測驗

JJS: Counting the Values (Part 2)

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