Dot operator
Objects are a collection of name/value pairs.
let object = {
address: {…},
name: “rick”,
display: function() { console.log(name + address.toString()); }
}
Using references (names), it can contain values:
– primitive
– object
– function (methods)
The objects sit in memory via a hex address. For example 0x294abcc9
The references also have a reference. For example, in our example above, object, address, name, display all are sitting on hex address memory.
Below, we create a new object have a reference ‘name’ point to it.
Then using brackets, we’ll specify a string that designates the name of the property we want to add. This is called computed member access.
For the property, we give it firstname.
1 2 |
var person = new Object(); person["firstname"] = "Rick"; |
Therefore, ‘firstname’ is now a property of our person object. It also is sitting on a hex memory. It is referencing a string literal with data “Rick”.
You can use another variable as a placement.
1 2 |
let someProperty = "lastName"; person[someProperty] = "Cao"; |
However, there is a cleaner operator on how to do this. And its called the “dot” operator.
The dot operator is a function:
1 |
person.firstName // Rick |
It takes firstName as the string parameter and uses it as the string with computed member access.
Associative is left-to-right
When you use dot operator, its associative priority is left to right.
1 2 3 4 5 |
let obj = new Object(); obj.name = "rick"; obj.address = {}; obj.address.name = 'haha'; obj.address.city = 'nowhere'; |
In our example, it’ll look at obj and then try to find the next string as a property. Hence, it’ll see obj, and try to see if it has an address property. We attached an object literal to the address property already so it finds it. Then it looks at the property name. However, ‘name’ is nowhere to be found so it attaches the ‘name’ property and assigns it to literal string ‘haha’.
console.log(obj.address.name); // haha
console.log(obj[“address”][“city”]); // nowhere
Object Literals
Object literal is the same as ‘new Object’. It creates a single instance of an object.
It is created like so:
1 2 3 4 |
let a = { firstname: "rick", // name/valuepair lastname: "cao", }; |
this is essentially same as
1 2 3 |
let a = new Object(); a.firstname = 'ricky'; a.lastname = 'cao'; |
You can also have nested objects like so:
1 2 3 4 5 6 7 8 |
let a = { firstname: "rick", // name/valuepair lastname: "cao", address: { city: "CA", address: "123 Main St" } }; |
Now, we can pass these objects into function parameters to be used. This is because function parameters take references to objects. Thus, when you pass in this literal object, you are literally giving it the object reference. That is why you can pass in whatever object you want, and then use that object within the function.
You can also create literal objects on the fly in the function parameter space like so:
1 2 3 4 |
let ans = greetings({ firstname: "ted", lastname: "bundy" }); |
You are creating something on the fly when the function is called.