- Overview
- Transcript
2.1 Primitive Data Types
Not everything in JavaScript is an object. In this lesson, you'll learn how JavaScript handles primitive types like integers and booleans.
1.Introduction1 lesson, 01:12
1.1Introduction01:12
2.The Basics2 lessons, 11:11
2.1Primitive Data Types06:33
2.2Creating Objects04:38
3.Creational Patterns4 lessons, 37:48
3.1The Factory Pattern08:36
3.2The JavaScript Data Type14:10
3.3The Class11:41
3.4Privacy03:21
4.Inheritance Patterns3 lessons, 28:15
4.1Parasitic Inheritance11:06
4.2Prototypal Inheritance11:07
4.3Class Inheritance06:02
5.Applying What You Know2 lessons, 25:13
5.1A Custom Event System11:53
5.2A Very Simple Toolbar13:20
6.Conclusion1 lesson, 00:43
6.1Conclusion00:43
2.1 Primitive Data Types
You've probably read or heard the phrase that everything in JavaScript is an object. I know I certainly have, and in fact, I have said those exact words. But the fact of the matter, is that not everything in JavaScript is an object. It's just easy to say that, because it just glosses over the technicalities of the language. Instead, what we have are called primitive types, and then we have objects, or reference types. Now, a primitive type is a Boolean, a string, a number, undefined, null, or starting in AcmeScript 6, or 2015, whatever you want to call that, we now have symbol. Except symbol is different from the other primitive types. In that we don't have a literal for a symbol. Now, the thing about primitive values is that they don't have any properties or methods. And that can be a little confusing, because we can write code like this. We can have a string literal. And then, we can use the length property or any other property or method of a string. Now, behind the scenes JavaScript is actually doing this. It is taking our string value and wrapping that with a string object. So, it's calling the string constructor, of course, with the new keyword, it's passing in our string value and then it's using whatever property or method that we used. So, it creates this string wrapper object and then it destroys it, but it does so very quickly. And of course, it's completely transparent to us. So with that in mind, we can use the typeof operator in order to find what typeof object or value that we are working with. So, if we say typeof and then a string literal, this is of course going to give us a string. But if we do type of new string and then we pass in our hello literal, then this is going to give us an object. Now, look at this however. If we just call string. Without the new constructor, this is not going to give us an object. Instead, it's going to give us a string. So, there is a difference between calling string as a function and then calling string as a constructor. The string function is useful if you want to convert something to a string. But there's actually better and easier ways of going about that. And then finally, we can use the valueof method on an object. So if we say new String and then pass in hello and then call valueof, this is going to give us a string. Because the value of this string object is a string. Well, let's do the same thing with a number. Let's do typeof 10. This is, of course, going to return number. And if we do typeof new number and then we pass in a numeric value, this is going to give us an object. But if we call just the number function. Instead of using it as a constructor, this is going to give us a number. And of course, if we do the value of method on a number object, then that is going to give us a number as well. So, whenever you use a primitive value as an object, JavaScript is going to create a temporary wrapper object around that value, it will use whatever property or method that you have used, and then it will destroy that object. And that is very apparent if you try to assign a property to a primitive value. So, let's create a variable called hello. And it's going to contain the value of hello. And then we can do, hello.foo equals something. And then, we can say hello.foo to try to get that value. Now, nothing that I wrote here is invalid. We aren't going to get an error whenever line 23 executes, and we aren't going to get an error when line 24 executes. However, we are going to get undefined because the foo property does not exist on the temporary object that was used for this Hello value. So, whenever you use a primitive value as an object, JavaScript is going to create a temporary object and then destroy that object. Now, if you're like me you're starting to think, ha, if I start using objects then I can increase performance, but that's not the case. Do not do that. Use primitive values any time that you need to use a primitive value. The only thing that you're going to gain are more keystrokes, and of course, anybody who looks at your code is going to think what in the world is this person thinking. This is very fast as is. So, don't do anything different. Now with saying that, there are going to be times when you do have an object. But JavaScript is smart enough. It's going to coerce that object into whatever value is appropriate for the operation. Like for example let's create a number objects. So, new number. And let's just have the value of 10 here. And if we want to use this num objects in an arithmetic operation, we can. So, let's say 10 + num and we are assigning that to the sum variable. Now, this is going to be 20 because JavaScript is going to take this num object, it's going to coerce it into a number, and then use that in the arithmetic operation. Now, for the sake of completeness, let's look at this. Let's say num.foo = foo, and then we would try to access that foo property. Now in this case, we are actually going to see the value of foo for this property. The reason being because this num variable is a number object. And once you have an object, you can dynamically add properties to that object. However, let me reiterate, do not use wrapper objects in place of primitive values. It can actually be dangerous to do so. For example, let's create a Boolean object. So we will new up Boolean, let's pass false to the constructor. And then let's say, if value and then let's just do something in here that would be obvious. So, let's alert something, foo, in this case. Now if this code was to execute, we would see an alert box saying foo. The reason being because a value is an object and in JavaScript objects are truthy values. So, it's not going to coerce this into the actual Boolean value of false. Is going to see that we use an object in the condition that the object is truthy. So therefore, the condition is true. So, use those primitive values. Only use the wrapper objects in edge cases, where it only makes sense to do so. In the next lesson, we're going to start creating objects using object literal notation.