Transcript from the "Boxing" Lesson
>> Kyle Simpson: I referenced a bit earlier a sort of magical behavior like accessing properties on primitive values. Remember, you access a length on a primitive string or some method on a primitive number, for example. So how does that work? How is it that we access the .length of some string value here?
[00:00:23] This DOM elements value is always a string. So how do we access that .length? Well, it turns out that's called boxing. It's a form of implicit coercion. It's not called out in the same way in the abstract operations. But I think it absolutely in spirit is an implicit coercion.
>> Speaker 2: Is that where this notion of everything is an object comes from?
>> Kyle Simpson: It is, that's exactly what I meant when I was talking in the intro, that people think that everything's an object.
[00:01:36] And it turns out that things can behave as objects, but that doesn't make them an object. This is not an object. It is a primitive string that has an optimization in it where you can access a property as if it was an object. So these are all examples of coercion that are happening in your programs.
[00:02:10] You're always gonna have a string that you need to treat as a number or a number that you need to treat as a Boolean. You are going to have to deal with conversions, aka coercion.
>> Kyle Simpson: It's absolutely necessary, so, you're gonna use it whether you're admitting it or not.
[00:02:27] Cuz you really have no choice.