Section 3 - Types and Operators
in# Section 3 - Types and Operators
Objects and Functions
Constructing a new object
Whats happening here:
[] is an operator that accesses a property of an object
= is us assigning a property.
[] used to modify, add, or access an objects property.
notice that firstnameperson and lastnameperson are used to dynamically define the properties of our object. Interestingly, trying to access more directly by the property I defined didnt seem to work.
A more convenient way of accessing a member object property.
Why is this can be powerful:
We can define objects within other objects.
What we have done is defined an object within another object
AKA subobjects or child objects
This is pretty cool because it allows us to structure and organize properties in a very efficient way.
We can use this to build larger scaled models.
The syntax is very nice.
An alternative, possibly easier way of assigning object properties
Dot operators are ususally preferred.
Objects and Object Literals
This is performs that same thing as object new.
There are different ways of doing things. So what does this mean?
I think this is the better way to do thing when beginning to initialize things. Watch.
Here we've constructed new object, a name value pair all on a single line.
The other approaches studied earlier, are probably better as commands; that letting the program modify things on the spot.
easy peezy
We can write functions that utalize objects.
Whats happening here:
An object is created using literals
A function which, and I have to get used to this, takes ANY variable (dynamically defined), that greets a person.
greet(Tony) passes in the OBJECT directly as a variable, so "hello" + Tony.firstname is outputed.
We can also create objects on the fly:
May have to get used to this. But Mary is a new object created on the function call. We passed in a function an object that was what?
function greet(person) takes a person, which is dynamically defined, and firstname is defined in Tony. Why does this work?
My understanding of this so far:
When we pass in a variable to a function, because everything is handled dynamically, it is created on the fly. So greet('dan'), is no different than greet ([some object]). Both are objects -> is just that strings are more primitive and easier understood. But objects you've defined have properties, so it complicates things a bit.
I think I got it now. This helped clear things up for me. Lets take a look at this again, slightly modified. Variable names dont make sense, but whatever - the point will get across.
So what this means:
dan is the name of our object. But does not always have to be dan. Something more abstract like person would have made more sense. Because dan was constructed as the object originally, greet(dan) is understood because the object properties have been defined.
greeting mary, on the other hand does not because even though we've defined a NEW object that HAS NOT overwritten the previous (dan), we have not defined a property for that new object that the function understands (person.address.city).
To fix this, we define the property.
And to prove that it has not become overwritten, we can greet dan again.
The most important take away from this I think is that object can be created very quickly, and in quiet high level manner. For example, in greeting mary, we are not talking about the properties from object dan. Rather, we are really defining an entirely new object that may or may not have the same properties as dan.
Faking Namespaces
A namespace is a container for variables and functions. Consider
To avoid these kinds of conflictions, lets write a container, or object that specifies our information down.
Or if we want to have things even more compact, we can do this:
This is want I was refering to earlier about global variables. This is good practice because it keeps them where they belong rather than in the global namespace.
JSON and Object Literals
AKA js object notation
In the earlier days, data was sent across the internet using XML. This was however, inefficient, and had a lot of unnessessary characters, and hence more bandwidth required for this characters. The solution was to take the way js objects where sent. So we now send data via the JSON format. Looks like:
Because JSON is so similiar to js, js has some built in capabilities related to JSON. And we can do so like this:
The JSON file will be to what you see above.
Furthermore, we can do the reverse. Note that the format cannot have \n characters.
But we will notice that backtoObject will be the same as myObject.
JSON is a popular method of just sending over data - it is used interchangably with the term AJAX.
It also cannot contain functions, because it is not nessessarly data per-say.
Functions are Objects
Fundamental concept
First class functions: everything you can do with other types, you can do with functions.
Special type of object
Stored in memory
Functions dont require a name, aka an anoyomous function
Consider this. We've defined a property to a function, and the notation we've used was the same as when treating other objects. And we can prove that the property is in memory.
Function statements and Function Expressions
The difference between expressions are statements
An expression returns a value. For example.
Statements dont return anything. But they can evaluate expressions. For example
Functions can be statments as well. Basically, from from C, they return 'void'
Is stored in memory, but does not return a value.
Function Expressions:
Because functions are objects, we can do something like this
This is what is known as an anoyomous function, because it does not contain a name property. Instead, functionObjects stores the memory location of function(). The function part is an expression because it results in a value.
And now as object, that has unique function properties, we can do something like this.
Note here that it was invoked about its declaration. While normally, we can call functions before they're delclaration, here because we are using the assignement operator, execution is handled after the memory allocation phase.
functionObject is undefined initially. The variable holds memory, but is not assigned anything only after its execution phase.
We can also do something really interesting.
And now even function objects, which behave the same as literals.
Whats happening: variables are dynamic, we can pass in any kind of object.
This is useful because it is immediately invoked. Why pass it into a variable when you are only going to use it once, for example.
It is also useful because an immediately unvoked function by nature also wraps around a new scope for the variables that are created within it.
We can use this trick in building safe modules, or wrapped classes that will not interfear with other variables.
Note the name is not nessessary for a function, but it can have it. It just does not have to.
Which means that we can do something like this...
Pretty crazy. Its like reversing the procedure. What we've done is constructed an object right on the fly,and passed it into a function call. That function took this object, and invoked it.
This is known as functional programming. We can treat anything as a variable. Everything essentially becomes an object.
Value vs Reference
It we are using a primative type, and we pass in that type to another function, then we are copying the variable, because we are using a new location.
Primative types (or single value types) are automatically copied, and objects are passed by reference.
That mean there is a single entity or pointer address for objects, and that any changes within these object will affect all other objects that are pointed or derived from it.
New location in memory is created by value.
In addition copies are independent. There references do not affect one another, because they don't have any. Instead, they are treated are individual locations in memory.
References, on the other hand, are like desktop shortcuts. You have one real application, and as many shortcuts as you want to that particular real application. This way, we avoid wasting memory.
The most important thing to note about copies, is that their addresses are unique. And this is what defines a value as unique.
One more thing to point out about references. If we delete the real deal, the other references will have broken links, so it would not have any access.
Assignment to an object uses reference. All object interact by reference.
Here, because objects are passed by reference, it does not matter what reference we use - the important thing to note is that they are the same object. Modify one, and you modify the other as well.
So objects are essentially single units in space and from what I known right now, there is no way to create a copy of that object like you can do with C. To get another object that is different you have to construct a new one.
Keyword: immutate. To change something. Here, we mutate our object.
We can demonstrate pass by reference through a function call, we do something like this:
If we do this:
Then c is a new object because their link was not established (c was not set = b).
Other programming language give us the option of deciding whether or not something should be passed by reference or by value. Js does not give us this option. Objects are references and primatives are by value.
Objects Functions and This
What is this? Its a pointer to an object that can change depending on the circumstance.
We've already identified that this under the global enviroment is the window object.
The first thing to notice is the even functions' 'this' will all access the global window object namespace.
And we can add an object property to this namespace.
So the question now is how can we modify create a new context for 'this' other than the global one.
The answer is when we create a new object.
First thing to note is the we've invoked an annoymous function through an object. This, in this case was created under a new execution context, so now points to a new namespace because of the context it was created under. This is useful because we can use this very convieniently rather than having to create an new object simply to modify something.
Sigh... the mistakes I've been making.
You see? Every new object has a this, within its own context, and we can use it to modify object properties.
This next bit identifies what others feel is wrong with js.
Whats is going on here? Is this not pointing to the same object? I would have expected sam.
As it turns out, every internal function of an object has this same kind of problem. Maybe its because of the concept of functional programming, and how functions are treated ecactly like objects.
'a' object, i believe created a new context for this. So this.name was a new property defined for the new object.
To fix this, we have to understand how references work. var self has stored a reference to the object this, which was created under the context of newObject. Now, whenever we reference self, we will be referencing newObject's properties. Now, executing a() will indirectly modify newObjects' name.
Using this, modifies the context in which is was created under.
Arrays
First thing to notice is that array can hold any kind of objects in their container. Even functions, objects. In addition, anything within an array can be invoked whenever.
They just have to be accessed, and called.
The syntax comes pretty simple too.
Looping through arrays
Arguements and Spread
arguements aka parameters.
One very interesting thing about functions in javascript is that their arguements or parameters still have their memory allocated in the first phase, even if they are not called.
And even if we go provide an arguement, js is flexible enough allow you to do these things. And the order
And when we want we can coerse to a default value - like before (albiet this doesnt introduce any new concepts)
Now for the arguements keyword.
What the arguements keyword does is it lists the arguements that where passed to it.
Notice that it looks very similiar to an array. Although it is not exactly an array - it acts enough as an array to the point where we can use it as an array under most circumstances.
And one practical application to this would look like something to this:
Where arguments considers undefined to not be an arguement.
An like arrays, they can be accessed directly;
Last updated
Was this helpful?