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.
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:
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
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.
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.
Javascript variables may contain two different types of data:
The values from five primitive types: Undefined, Null, Boolean, Number, and String are primitive value.
Reference values are object references.
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.
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.