Categories:

JavaScript Kit > JavaScript Reference > Here

Object object

Last updated: Feb 18th, 2007

You can define your own objects in JavaScript, with custom properties and methods.

Constructor:

var myobject=new Object()

var myobject={ } //object literal. Supported in JavaScript 1.2

Related Tutorials

Properties

Properties Description
constructor Specifies the function that's the constructor for the object.

Methods

Methods Description
hasOwnProperty(prop) Returns true if an object has a property with the name as indicated by the parameter prop. Returns false if the property doesn't exist or is inherited from the prototype chain versus a direct property of the object.

var dog=new Object()
dog.age=5
dog.hasOwnProperty("age") //returns true
dog.hasOwnProperty("favoritefood") //returns false, property doesn't exist
dog.hasOwnProperty("toString") //returns false, as toString property is inherited

isPrototypeOf(o) Returns true if one object is the prototype of another . Use this method to detect the class of an object (o).

Example 1:

var fruits=["apple", "grapes"]
Array.prototype.isPrototypeOf(fruits) //returns true, "fruits" in an array
Function.prototype.isPrototypeOf(fruits.slice) //returns true, fruits.slice() is a function

Example 2:

function Car(){
//Car class here
}

function Toyota(){
//Toyota object here
}

Toyota.prototype=new Car() //inherit from Car class
var Prius=new Toyota //instance of Toyota object
Car.prototype.isPrototypeOf(Prius) //returns true, Prius is an instance of Toyota which inherits from Class Car

propertyIsEnumerable(prop) Returns true if the entered property name of this object is enumerable using for...in Returns false if either property doesn't exist, or if the object inherited the property from the prototype object. All user defined properties to an object are enumerable, while in general, built in properties/methods are not.

var mycar=new Object()
mycar.size="mid"

mycar.propertyIsEnumerable("size") //returns true for local user defined property
mycar.propertyIsEnumerable("toString") //returns false for prebuilt property
mycar.propertyIsEnumerable("cost") //returns false for non existent local property

toString() A method that's typically called implicitly by JavaScript whenever you call an object within the context in which a string value is expected, such as alerting an object: alert(mycar). By default it returns the object in the string representation [object Type].

A common task for programmers to do is to override this method with something more robust, especially for custom objects. the default returned value is less than descriptive of the object. For example:

function records(){
this.records=[]
for (var i=0; i<arguments.length; i++)
this.records.push(arguments[i])
}

records.prototype.toString=function(){
return "All records: "+this.records.join(" | ")
}

var myrecords=new records("George", "Edward", "Sue")
alert(myrecords.toString()) //alerts "George | Edward | Sue"

 

valueOf() A method that's typically called implicitly by JavaScript whenever you call an object within the context in which a primitive value is expected. Rarely if ever called explicitly.

Reference List

Partners
Right column

CopyRight © 1998-2009 JavaScript Kit. NO PART may be reproduced without author's permission.