Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Object-Oriented JavaScript
  • Table Of Contents Toc
Object-Oriented JavaScript

Object-Oriented JavaScript

4.5 (48)
close
close
Object-Oriented JavaScript

Object-Oriented JavaScript

4.5 (48)

Overview of this book

Table of Contents (18 chapters)
close
close
Object-Oriented JavaScript
Credits
About the Author
About the Reviewers
Preface
2
Built-in Functions
4
Regular Expressions
5
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

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Object-Oriented JavaScript
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon