Programming

JavaScript Object Types

There are 3 types of objects in JavaScript.

  1. Native : eg String, Math, Number, Object. Always start with Uppercase letter.
  2. Host : eg window, document , frames. Always lowercase. Provided by browser.
  3. User-Defined : programmer defined. Must follow JS variable naming conventions.

The Object object is the base object for all native and user-defined objects.
The Object object has a dozen of properties and methods for example, constructor, toString(), and valueOf().

You can create an Object object as well:

var myObject = new Object();

When you want to get a uniform feedback about an object’s names and content, the best way is to ask for their string version. Use the object’s methods of toString(), toLocaleString(), and valueOf().

For native objects the toString(), toLocaleString(), and valueOf() will return the value of the object.
Ex,

var myNum = new Number(35);
alert(myNum.toString); //alerts 35

For user defined objects, the toString()toLocaleString() and valueOf() ALWAYS returns [object Object].

function Employee() {
this.dept = "HR";
this.manager = "John Johnson";
}

alert((new Employee).toString()); // Alerts Object

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