Object Basics in JavaScript
What’s an object in JavaScript? Everything is an object in JavaScript, except primitive types. An object can have both state and behavior including properties and methods. Let’s say we just bought a new house, and in the backyard was a bunch of weird looking rocks. All of a sudden a carrier pigeon stops by, with a note from our neighbors up north, that the world is about to end due to the change of the seasons, and the only way to stop it involves these rocks in our backyard! It’s only certain kind of rocks though, it needs to be volcanic glass, have a mohs-scale of 6, and it immediately melts ice. To the JavaScript console!
There are three ways to create an object in JavaScript. The first would be making an object literal.
var dragonGlass = {
category: "volcanic glass",
mohs: 6,
ice_melting: true
}Classic, but what else could we do? We can also create an instance of Object using the new keyword, and then assign properties using either dot notation or bracket notation. What’s the difference between these two? With bracket notation we can assign properties and we can also assign a variable to a property name and JavaScript will still accept it. With dot notation you can only use the explicit name of the property, and only string types.
var dragonGlass = new Object();
dragonGlass.category = "volcanic glass"
var rocksProp = "category"
console.log(dragonGlass[rocksProp]); // "volcanic glass"The final way to create an Object is to use a constructor, where you can start to do a lot of cool stuff as well like assign functions and define scope.
var dragonGlass = function() {
this.category = "volcanic glass";
this.mohs = 6;
this.ice_melting = true; this.destroy = function(white_walker) {
return (white_walker + "has been destroyed. Nice.");
}
}
For more about objects, check out MDN, as close to The Citadel as we’ll get in this lifetime.

