Objects in Javascript

Objects in Javascript are free form which means that we can add/remove fields and methods whenever we want. They are not bound to a particular class (like in Java, in fact there is no class concept in JS).

Easiest way to create a object is object inline like – “var obj = {};” Objects can also be created via object literal

 
// create object inline
var myObj = {}; 
// FREE FORM: dynamically attach property to the above object
myObj.prop1 = "Hello"; 
console.log(myObj);
// Prints the above attached property
console.log(myObj1.prop1); 
// If we try to access the property which is not declared in object, we get it as 'undefined'
console.log(myObj1.prop2); 
delete myObj1.prop1; // FREE FORM: delete property from an Object

Properties and Access Specifiers
All properties within object are public as they dont come with any access specifier. Properties of an object can be accessed in 2 ways
– Using dot notation
– Using square brackets
When to use dot notation vs square brackets?

  • Use [] notation
    • when property is a reserved keyword or invalid identifier
    • when property name is dynamic
    •  var myObj1 = {
      "prop1": "Hello",
      "prop2": 10,
      "1": "one"
      }
      console.log(myObj1.1); // will result in error
      var propertyName = "prop2";
      console.log(myObj1.propertyName);
      
  • Prefer DOT NOTATION over [] NOTATION because JS Engine can do some optimizations upfront with DOT NOTATION. And DOT NOTATION is also faster compared to [] approach.

Two objects can be compared with ‘===’ operator
var myObj2 = myObj1;
(myObj1 === myObj2) -> returns true

Objects with undefined and null
If we dont define a property then its gonna be undefined, but if we wanna define a property with empty value then we initialize it with null value.

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

Mawazo

Mostly technology with occasional sprinkling of other random thoughts

amintabar

Amir Amintabar's personal page

101 Books

Reading my way through Time Magazine's 100 Greatest Novels since 1923 (plus Ulysses)

Seek, Plunnge and more...

My words, my world...

ARRM Foundation

Do not wait for leaders; do it alone, person to person - Mother Teresa

Executive Management

An unexamined life is not worth living – Socrates

Diabolical or Smart

Nitwit, Blubber, Oddment, Tweak !!

javaproffesionals

A topnotch WordPress.com site

thehandwritinganalyst

Just another WordPress.com site

coding algorithms

"An approximate answer to the right problem is worth a good deal more than an exact answer to an approximate problem." -- John Tukey