-
Book Overview & Buying
-
Table Of Contents
Object-Oriented JavaScript
Object() is a constructor that creates objects, for example:
>>> var o = new Object();
This is the same as using the object literal:
>>> var o = {}; // recommendedYou 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.constructorString()
>>> 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.
|
Property/Method |
Description |
|---|---|
|
|
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 |
|
Property/Method |
Description |
|---|---|
|
|
Points back to >>> Object.prototype.constructor === Object true >>> var o = new Object(); >>> o.constructor === Object true |
|
|
Returns a string representation of the object. If the object happens be a >>> var o = {prop: 1};
>>> o.toString()
"[object Object]" >>> var n = new Number(255); >>> n.toString() "255" >>> n.toString(16) "ff" |
|
|
Same as |
|
|
Returns the >>> 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 |
|
|
Returns >>> var o = {prop: 1};
>>> o.hasOwnProperty('prop')
true >>> o.hasOwnProperty('toString')
false |
|
|
Returns >>> var s = new String('');
>>> Object.prototype.isPrototypeOf(s)
true >>> String.prototype.isPrototypeOf(s) true >>> Array.prototype.isPrototypeOf(s) false |
|
|
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 |
Change the font size
Change margin width
Change background colour