Deep JavaScript Foundations, v3

Fundamental Objects

Kyle Simpson

Kyle Simpson

You Don't Know JS
Deep JavaScript Foundations, v3

Check out a free preview of the full Deep JavaScript Foundations, v3 course

The "Fundamental Objects" Lesson is part of the full, Deep JavaScript Foundations, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle defines fundamental objects in JavaScript and foreshadows why it is wise to use them selectively.

Preview
Close

Transcript from the "Fundamental Objects" Lesson

[00:00:00]
>> Speaker 1: In addition to our primitive values, we also have fundamental objects. And by the way, this is a terminology that I think was somewhat recently introduced in this spec. And these used to be referred to as built-in objects or maybe even native functions. I've referred to these historically as native functions.

[00:00:18]
I think they're more commonly now referred to as fundamental objects. So whichever phrase works for you, pick and choose. But if you're reading this spec, and you see something like a fundamental object, that's where this comes from. So what are these fundamental objects, are they types? Sort of, but not really.

[00:00:35]
This is like the kind of bolted on object orientedness of JavaScript, the almost Java-like mutation of JavaScript where we have in all of those cases where we have primitive values, we now also have object representations with similar behaviors. Like in Java when you want to make a string and you call it new capital S string, we have things like that in JavaScript.

[00:01:01]
I don't think you should use them, but I do think you should understand them. So these are the ones where you really should absolutely use the new keyword, Object, Array, Function, Date, Regular Expression, Error. If you need to construct an object of that fundamental type, then use new Object, or new Array, or new Function.

[00:01:27]
Probably the key one here that I would use would be new Date, for example. But there are other ones that are fundamental objects which could be used with new, but I'm gonna tell you you should definitely not use them with new, and that is these three, string, number, and Boolean.

[00:01:44]
They can be used with new to construct the objects of this form. You should absolutely never do that. You should use them only as functions, not as constructors. And when we talk about later in coercion and you'll understand the usefulness of them. But String, Number, and Boolean when used as a function coerce any value to that respective primitive type.

[00:02:07]
That is a far more useful utility of those than the fact that they can construct this weird Frankensteiny object. So an example here again Date, I would say new Date to construct the date object why because there is no date literal. If you want to make dates, you have got to do new Date.

[00:02:28]
That might be the only reason why I think we should keep the new operator around because I need to make dates from time to time and that is the only way to do it. I do hope that maybe someday they add a date literal. But, we don't have that yet.

[00:02:41]
So I can construct dates, but if I have something that is not already a string, for example, if I got a JSON object back that had myGPA from college in it, here I am telling you what myGPA in college was. If I got back a JSON object, that was my transcript from college and it had myGPA in it, but I needed to represent it as a string, capital S string function does that and does it well.

[00:03:07]
It converts it to that string representation.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now