JS—Objects

Objects are special variables in JS, it could contain all of variables types, including function.

Capture2.PNG

an simple example with their names.

Let’s continue,

how to show the specific property value, 2 ways:

Capture2

it is also fine to use retrieve variable,

Capture2.PNG

How to add new property and property value,

Capture2.PNG

Notice: we do not need *var* here. So result is here :

Capture3.PNG

Above is simple object. Let’s have a little bit complex object.

Capture2.PNG

Function expression can be stored in a variable, and the variable can be used as a function :

Capture3.PNG

Those functions  are anonymous function (function without name). Because they do not need names, they are invoked using the variable name.

(Learned from w3schools JS Function parts)

Generally, how to call a function expression ?

Capture2.PNG

We need a new variable “z” to store the function result, and call the function variable name which is “calcuateAge”and pass parameter”yearOfBirth” and “1992”.

However when this function expression is in object, how to call it ?

Capture2.PNG

Now I got a new idea,

Capture2.PNG

I will use keyword “this”,

Capture2.PNG

What is “this” in our example ?

Capture2

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s