1.1 Objects vs. Primitives
So far, you have primarily worked with the following data types:
- string: a sequence of characters
- numbers: stores (rational) numbers
- boolean: only has two values: true or false
(See more in Section 1.5)
These are all primitive data types. They form the building blocks of more complex data types in Javascript, called objects. Any data type that is not a primitive type is classified as an object.
Recall from Section 1.5 and Section 5.5 that Objects have properties that come in
key:value
pairs:let myCubeObject = { sideLength: 8; color: “yellow”; }
That’s all we know about objects so far. What makes objects so different from primitives? Are they a collection of primitive data types (like in the example above, myCubeObject store two values in primitive data types)? Objects are far more complex and have many more functions than primitives. For example, objects may have methods:
let myCubeObject = { sideLength: 8; sayHi() { console.log(“Hi!”); } }
We may call the sayHi method in the following way:
myCubeObject.sayHi();
What exactly is going on behind the scenes when we create and use objects? This has to do with the object prototype chain, which we’ll explore in the next section.
Next Section
1.2 Object Prototype ChainCopyright © 2021 Code 4 Tomorrow. All rights reserved.
The code in this course is licensed under the MIT License.
If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.