Create Object type variable in Javascript








Objects in Javascript groups data and functions.

Objects are created by using the new operator.

var o = new Object();

You can create your own objects by creating instances of the Object type and adding properties and methods to it.

Object instance

The Object type in Javascript is the base for all other objects.

All of the properties and methods of the Object type are also present on other, more specific objects.

Each Object instance has the following properties and methods:

  • constructor - The function used to create the object.
  • hasOwnProperty(propertyName) - tells if a property exists on the object instance (not on the prototype). The property name must be specified as a string (for example, o.hasOwnProperty("name")).
  • isPrototypeOf(object) - Determines if the object is a prototype of another object.
  • propertyIsEnumerable(propertyName) - Indicates if the given property can be enumerated using the for-in statement. The property name must be a string.
  • toLocaleString() - Returns a string representation locale to execution environment.
  • toString() - Returns a string representation of the object.
  • valueOf() - Returns a string, number, or Boolean equivalent of the object. It often returns the same value as toString().




instanceof

To identify what type of object it is, Javascript provides the instanceof operator, which is used with the following syntax:

var booleanResult = variable instanceof constructor

Example

The instanceof operator returns true if the variable is an instance of the given reference type.


//(person instanceof Object);   //is the variable person an Object?
//(colors instanceof Array);    //is the variable colors an Array?
//(pattern instanceof RegExp);  //is the variable pattern a RegExp?
//from  w w  w.ja v a 2s  .co m
var person = new Object();
var colors = new Array();
console.log(person instanceof Object); 
//is the variable person an Object? 
console.log(colors instanceof Array); 
//is the variable colors an Array? 

All reference values are instances of Object, so objectReference instanceof Object always returns true.

Similarly, primitiveTypeVariable instanceof Object will always return false.

The code above generates the following result.





typeof vs instanceof

The typeof operator tells if a variable is a primitive type. It can determine if a variable is a string, number, Boolean, or undefined.

If the value is an object or null, then typeof returns "object".


var s = "XML";
var b = true;
var i = 2;
var u;
var n = null;
var o = new Object();
//from ww  w  .  j  ava2 s .c  o  m
console.log(typeof s);   //string
console.log(typeof i);   //number
console.log(typeof b);   //boolean
console.log(typeof u);   //undefined
console.log(typeof n);   //object
console.log(typeof o);   //object

The typeof operator also returns "function" when used on a function.

The code above generates the following result.

Primitive And Reference Values

Javascript variables may contain two different types of data:

  • primitive values
  • reference values

The values from five primitive types: Undefined, Null, Boolean, Number, and String are primitive value.

Reference values are object references.

Copying Values

Primitive and reference values act differently when copied from one variable to another.

When assigning a primitive value, the value is copied from one variable to the new variable.


var num1 = 5;
var num2 = num1;
console.log(num1);
console.log(num2);

The code above generates the following result.

Copying address

When assigning a reference value, the variable value is also copied into the new variable.

The value from reference type is an address to an object.

Once the operation is complete, two variables point to the same object, so changes to one are reflected on the other.


var obj1 = new Object();
var obj2 = obj1;
obj1.name = "XML";

console.log(obj2.name);    //"XML"

In this example, the variable obj1 is filled with a new instance of an object. This value which is address to the object is copied into obj2, both variables are now pointing to the same object. When the property name is set on obj1, it can later be accessed from obj2.

The code above generates the following result.