Object:

Creates an object wrapper.

The Object constructor creates an object wrapper for the given value. If the value is null or undefined, it will create and return an empty object, otherwise, it will return an object of a type that corresponds to the given value.

When called in a non-constructor context, Object behaves identically.




Using Object given undefined and null types

The following examples store an empty Object object in o: var o = new Object();

var o = new Object(undefined);

var o = new Object(null);
Using Object to create Boolean objects

The following examples store Boolean objects in o:

// equivalent to o = new Boolean(true);
var o = new Object(true);

// equivalent to o = new Boolean(false);
var o = new Object(Boolean());




toString( ) : String
Returns a string representation of the object.


Every object has a toString() method that is automatically called when the object is to be represented as a text value or when an object is referred to in a manner in which a string is expected. By default, the toString() method is inherited by every object descended from Object. If this method is not overridden in a custom object, toString() returns "[object type]", where type is the object type. The following code illustrates this:

var o = new Object();
o.toString(); // returns [object Object]
You can create a function to be called in place of the default toString() method. The toString() method takes no arguments and should return a string. The toString() method you create can be any value you want, but it will be most useful if it carries information about the object.
The following code defines the Dog object type and creates theDog, an object of type Dog:

function Dog(name,breed,color,sex) {
this.name=name;
this.breed=breed;
this.color=color;
this.sex=sex;
}

theDog = new Dog("Gabby","Lab","chocolate","female");
If you call the toString() method on this custom object, it returns the default value inherited from Object:

theDog.toString(); //returns [object Object]
The following code creates and assigns dogToString() to override the default toString() method. This function generates a string containing the name, breed, color, and sex of the object, in the form "property = value;".

Dog.prototype.toString = function dogToString() {
var ret = "Dog " + this.name + " is a " + this.sex + " " + this.color + " " + this.breed;
return ret;
}
With the preceding code in place, any time theDog is used in a string context, JavaScript automatically calls the dogToString() function, which returns the following string:

Dog Gabby is a female chocolate Lab
toString() can be used with every object and allows you to get its class. To use the Object.prototype.toString() with every object, you need to call Function.prototype.call() or Function.prototype.apply() on it, passing the object you want to inspect as the first parameter called thisArg.

var toString = Object.prototype.toString;

toString.call(new Date); // [object Date]
toString.call(new String); // [object String]
toString.call(Math); // [object Math]






Methods:

create( proto, [propertiesObject] ) : Objectstatic

Creates a new object with the specified prototype object and properties.




Using propertiesObject argument Object.create:


var o;

// create an object with null as prototype
o = Object.create(null);


o = {};
// is equivalent to:
o = Object.create(Object.prototype);


// Example where we create an object with a couple of sample properties.
// (Note that the second parameter maps keys to *property descriptors*.)
o = Object.create(Object.prototype, {
// foo is a regular "value property"
foo: { writable:true, configurable:true, value: "hello" },
// bar is a getter-and-setter (accessor) property
bar: {
configurable: false,
get: function() { return 10 },
set: function(value) { console.log("Setting `o.bar` to", value) }
}})


function Constructor(){}
o = new Constructor();
// is equivalent to:
o = Object.create(Constructor.prototype);
// Of course, if there is actual initialization code in the Constructor function, the Object.create cannot reflect it


// create a new object whose prototype is a new, empty object
// and a adding single property 'p', with value 42
o = Object.create({}, { p: { value: 42 } })

// by default properties ARE NOT writable, enumerable or configurable:
o.p = 24
o.p
//42

o.q = 12
for (var prop in o) {
console.log(prop)
}
//"q"

delete o.p
//false

//to specify an ES3 property
o2 = Object.create({}, { p: { value: 42, writable: true, enumerable: true, configurable: true } });


q
42



defineProperties( obj, props ) static
Defines new or modifies existing properties directly on an object, returning the object.


Object.defineProperties(obj, {
"property1": {
value: true,
writable: true
},
"property2": {
value: "Hello",
writable: false
}
// etc. etc.
});
console.log(obj);



OUTPUT:

Object {property1: true, property2: "Hello"}


freeze( obj ) static


Freezes an object: that is, prevents new properties from being added to it; prevents existing properties from being removed; and prevents existing properties, or their enumerability, configurability, or writability, from being changed. In essence the object is made effectively immutable. The method returns the object being frozen.

Object.defineProperties(opl, {
"property1" : {
value : true,
writable : true
},
"property2" : {
value : "Hello",
writable : false
}

});
console.log(opl);

Object.freeze(opl);

Object.defineProperties(opl, {
"property3" : {
value : true,
writable : true
},
"property4" : {
value : "Hello",
writable : false
}

});
console.log(opl);


OUTPUT:




Uncaught TypeError: Cannot define property: property3, object is not extensible.



getOwnPropertyNames( obj ) : String[]
Returns an array of all properties (enumerable or not) found directly upon a given object. ...
Returns an array of all properties (enumerable or not) found directly upon a given object.
Object opl = Object {property1: true, property2: "Hello", property3: true, property4: "Hello"}

console.log(Object.getOwnPropertyNames(opl));



OUTPUT:

["property1", "property2", "property3", "property4"]



seal( obj )static
It is similar to freeze except that if an existing property is configurable i.e. its writable proprty is true it can be changed.
Seals an object, preventing new properties from being added to it and marking all existing properties as non-configurable. Values of present properties can still be changed as long as they are writable.



getOwnPropertyDescriptor( obj, prop ) :

Returns a property descriptor for an own property (that is, one directly present on an object, not present by dint of being along an object's prototype chain) of a given object.

Parameters
obj : ObjectThe object in which to look for the property.
prop : StringThe name of the property whose description is to be retrieved.

A property descriptor is a record with some of the following attributes:

value The value associated with the property (data descriptors only).
writable True if and only if the value associated with the property may be changed (data descriptors only).
get A function which serves as a getter for the property, or undefined if there is no getter (accessor descriptors only).
set A function which serves as a setter for the property, or undefined if there is no setter (accessor descriptors only).
configurable true if and only if the type of this property descriptor may be changed and if the property may be deleted from the corresponding object.
enumerable true if and only if this property shows up during enumeration of the properties on the corresponding object.
Returns
MixedValue of the property descriptor.




Example:

For object --> Object {property1: true, property2: "Hello", property3: true, property4: "Hello"}

we execute the following method:

console.log(Object.getOwnPropertyDescriptor(opl,"property4")) ;
Output:

Object {value: "Hello", writable: false, enumerable: false, configurable: false}