JavaScript Kit > JavaScript Reference > Here
Object object
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 |
---|---|
__defineGetter__
JavaScript 1.5 feature, supported in FF, Chrome, Safari 3+, but not IE10 |
Method for getting the value of a property of a JavaScript object or function. In the past coders have defined their own getter and setter functions within an object to conveniently access and change data within an object- now you can do this natively and using more streamlined code.
The following defines two getter functions inside another to access the "private" properties of the later: function getarea(width, height){ When defining a getter method inside an object literal, the syntax is different, and should be in the form var myspace = { |
__defineSetter__
JavaScript 1.5 feature, supported in FF, Chrome, Safari 3+, but not IE10 |
Method for setting the value of a property of a JavaScript object or function. Lets extend the getarea() function above with a setter method for the private variable "w":
function getarea(width, height){ As you can see, when defining a setter method, the syntax is almost identical to a getter method, with the exception that its function accepts a parameter whose value is assigned to it by you when calling it. Getter/ Setter methods can also be defined on the prototype property of objects. The following defines a setter method on the Array object to modify the order of the array when set: Array.prototype.__defineSetter__("sorttype", function(x){ When defining a setter method inside an object literal, the syntax is different, and should be in the form var myobject = { |
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() |
isPrototypeOf(obj) |
Returns true if one object is the prototype of another . Use this method to detect the class of an object (obj).
Example 1: var fruits=["apple", "grapes"] Example 2: function Car(){ |
Object.keys(obj)
Supported in FF4+, IE9+, Chrome5+, and Safari 5+ |
Returns an array of all the enumerable property names of an object (though not those in its prototype chain). The order of the names is the same as that returned by using a for-in loop to retrieve the property names. For example:
var box={width:100, height:35, length:40}, boxvolumn=1 The difference between using To get all enumerable and non enumerable properties of an object, use Note: See Looping through an object's properties as well. |
Object.getOwnPropertyNames(obj)
Supported in FF4+, IE9+, Chrome5+, and Safari 5+ |
Similar to keys(obj) above but also returns the non enumerable properties of an object. All user defined properties of an object are enumerable, while in general, built in properties/methods are not.
You can use var stringprops=Object.getOwnPropertyNames(String) var stringprops=Object.getOwnPropertyNames(Array) Note: See Looping through an object's properties as well. |
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() |
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(){
|
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. |
Looping through object properties
There are a few ways to loop through an object's properties, depending on your needs.
For-in Loop
The for-in loop, supported by all browsers since the dawn of time, can be used to loop through both an object's enumerable properties, plus any enumerable properties inherited by the object's prototype chain.
var userprofile={name:'George', age:30, sex:'male', getage:function(){return this.age}}, props=[]
for (var p in userprofile){ //loop thru properties of object
props.push(p) //props will contain ["name", "age", "sex", "getage"]
}
Or:
function circle(r){
this.radius=(typeof r!="undefined")? r : this.radius //get radius of circle instance
this.area=Math.pow(this.radius,2) * 3.14
}
circle.prototype={
radius: 2, //default radius of circle
getparameter:function(){
return 2 * 3.14 * this.r
}
}
var circle1=new circle(), props=[]
for (var p in circle1){ //loop thru properties of object
props.push(p) //props will contain ["radius", "area", "getparameter"]
}
Object.keys(object)
Object.keys(object)
, supported in FF4+, IE9+, Chrome5+, and Safari 5+, lets you easily get the enumerable properties of an object, not including any inherited from the prototype chain. It returns the data as an array:
var userprofile={name:'George', age:30, sex:'male', getage:function(){return this.age}}
var props=Object.keys(userprofile) //props will contain ["name", "age", "sex", "getage"]
Cross browser version of Object.keys()
For browsers that don't support Object.keys()
, we can bridge that gap by implementing a cross browser version of Object.keys()
on our own that take advantage of different features of JavaScript depending on which ones the browser supports:
if (typeof Object.keys=="undefined"){
Object.keys=function(obj){
var props=[]
for (var p in obj){
if (obj.propertyIsEnumerable(p))
props.push(p)
}
return props
}
}
Object.getOwnPropertyNames(object)
Finally, there's Object.getownProperty(object)
, which is also supported in FF4+, IE9+, Chrome5+, and Safari 5+. Use this method to easily loop through all enumerable PLUS non enumerable properties of an object (those found in the prototype chain NOT included), which comes in handy for built in objects' properties, such as String, Math, or Array. The following loops through the Math object of JavaScript and calls on all of the trigonometry related methods of the object on a number:
var trigmethods=Object.getOwnPropertyNames(Math).filter(function(element, index, array){
return (/(acos)|(asin)|(atan)|(atan2)|(cos)|(sin)|(tan)/i.test(element))
})
for (var i=0; i<trigmethods.length; i++){ //loop through all trig related methods of Math
alert(Math[trigmethods[i]](2.5)) //call Math.trigfunction(2.5)
}
- JavaScript Operators
- JavaScript Statements
- Global functions
- JavaScript Events
- Escape Sequences
- Reserved Words