Book Image

Object-Oriented JavaScript

By : Stoyan Stefanov, Stoyan STEFANOV
Book Image

Object-Oriented JavaScript

By: Stoyan Stefanov, Stoyan STEFANOV

Overview of this book

Table of Contents (18 chapters)
Object-Oriented JavaScript
Credits
About the Author
About the Reviewers
Preface
Built-in Functions
Regular Expressions
Index

Object


Object() is a constructor that creates objects, for example:

>>> var o = new Object();

This is the same as using the object literal:

>>> var o = {}; // recommended

You can pass anything to the constructor and it will try to guess what it is and use a more appropriate constructor. For example, passing a string to new Object() will be the same as using the new String() constructor. This is not a recommended practise, but still possible.

>>> var o = new Object('something');
>>> o.constructor

String()

>>> var o = new Object(123);
>>> o.constructor

Number()

All other objects, built-in or custom, inherit from Object. So the properties and methods discussed below apply to all types of objects.

Members of the Object Constructor

Property/Method

Description

Object.prototype

The prototype of all objects (also an object itself). Anything you add to this prototype will be inherited by all other objects.

>>> var s = new String('noodles');
>>> Object.prototype.custom = 1;

1

>>> s.custom

1

Members of the Objects Created by the Object Constructor

Property/Method

Description

constructor

Points back to Object

>>> Object.prototype.constructor === Object

true

>>> var o = new Object();
>>> o.constructor === Object

true

toString(radix)

Returns a string representation of the object. If the object happens be a Number object, then the radix parameter defines the base of the returned number. The default radix is 10.

>>> var o = {prop: 1};
>>> o.toString()

"[object Object]"

>>> var n = new Number(255);
>>> n.toString()

"255"

>>> n.toString(16)

"ff"

toLocaleString()

Same as toString() but matching the current locale. Meant to be implemented by objects such as Date() and provide locale-specific values, such as different date formatting.

valueOf()

Returns the this object itself, but for other types of objects may return a different value. For example, Number objects return a primitive number and Date objects return a timestamp.

>>> var o = {};
>>> typeof o.valueOf()

"object"

>>> var n = new Number(101);
>>> typeof n.valueOf()

"number"

>>> var d = new Date();
>>> typeof d.valueOf()

"number"

>>> d.valueOf()

1208158875493

hasOwnProperty(prop)

Returns true if a property is an own property of the object or false if it was inherited from the prototype chain. Also returns false if the property doesn't exist.

>>> var o = {prop: 1};
>>> o.hasOwnProperty('prop')

true

>>> o.hasOwnProperty('toString')

false

isPrototypeOf(obj)

Returns true if an object is used as a prototype of another object. Any object from the prototype chain can be tested, not only the direct ancestor.

>>> var s = new String('');
>>> Object.prototype.isPrototypeOf(s)

true

>>> String.prototype.isPrototypeOf(s)

true

>>> Array.prototype.isPrototypeOf(s)

false

propertyIsEnumerable

(prop)

Returns true if a property shows up in a for-in loop.

>>> var a = [1,2,3];
>>> a.propertyIsEnumerable('length')

false

>>> a.propertyIsEnumerable(0)

tru e