Javascript Objects

Javascript variables:       var person = “John Doe”;

Javascript objects are variables too. But objects can contain many values.The values are written as name : value pairs.

eg. var person = {firstName:”John”, lastName:”Doe”, age:50, eyeColor:”blue”};

Object properties can be both primitive values, other objects, and functions.

Creating new objects:
1) Using an Object Literal
2) Using the JavaScript Keyword new
3) Using an object constructor

Javascript Properties:

1)  person.age
2)  person[“age”]
3)  x = “age”; person[x]

Delete properties:    delete person.age

Javascript Object Prototypes:

Every JavaScript object has a prototype. The prototype is also an object.
All JavaScript objects inherit their properties and methods from their prototype.

eg. Person.prototype.nationality = “English”;
eg. Person.prototype.name = function()  {
return this.firstName + ” ” + this.lastName;
};

Links to reference post : http://www.w3schools.com/js/js_object_definition.asp

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s