Javascript Objects and Classes

In Javascript, there are no class declarations in JavaScript.
Javascript objects are dynamic. At runtime, new properties and methods van be add and removed using “delete” operator.
All Javascript properties are public.
Accessing non-existing properties does not throws an exception but returns “undefined”.
Properties can be used in array form, like myObject[myProperty].

Property notation is good for:

  • accessing properties with reserved words or properties starting with numbers (myObject[“1234”], myObj[“new”] works, myObject.1234, myObj.new does not work)
  • accessing properties dynamicaly (myObject[myVar])

An object can be declared as:
var x = {};

var x = {
a = 1;
b = “string value”;
myMethod() {
return “a”;
}
}

is same as:

var x = {}
x.a = 1;
x.b = “string value”;
x.myMethod() {
return “a”;
}

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