please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 21
JavaScript Reference
Next
 

Object Object

Name

Object Object---features of all JavaScript objects

Availability

Navigator 2.0, Internet Explorer 3.0; enhanced in Navigator 3.0

Constructor

new Object();
new Object(value);    Navigator 3.0

Arguments

value

In Navigator 3.0, this optional argument specifies a primitive JavaScript value--a number, a Boolean, a string, or a function--that is to be converted to an object.

Returns

If no value argument is passed, this constructor returns a newly created object, which has no properties defined. If a primitive value argument is specified, then the constructor creates and returns a Number, Boolean, String, or Function object wrapper around the primitive value.

Properties

constructor

A read-only reference to the JavaScript function that was the constructor for the object. This property is not defined in Navigator 2.0 or Internet Explorer 3.0.

Methods

assign()

A method, which, if defined, is used to implement the JavaScript assignment operator (=).

eval()

Evaluate a string of JavaScript code in the context of the given object. In Navigator 2.0 and Internet Explorer 3.0, eval() is a standalone function, rather than a method of Object.

toString()

A method, which, if defined, is used to convert an object to a string.

valueOf()

Return the primitive value of the object, if any. For objects of type Object, this method simply returns the object itself. For other object types, such as Number and Boolean, this method returns the primitive value associated with the object. This method was added in Navigator 3.0.

Description

The Object object is a built-in data type of the JavaScript language. It serves as the "superclass" for all other JavaScript objects, and therefore methods of the Object object are methods of all other object types. Similarly, the behavior of the Object object is shared by all other object types. The basic behavior of objects in JavaScript is explained in Chapter 7, Objects.

A number of the Object methods listed above are unusual in that they are not predefined, and are not intended to be invoked by the JavaScript code that you write. Instead these are methods that you can define for any object, and that will be invoked by the JavaScript system at appropriate times, to perform some sort of operation on the object. The toString() method is an example: once you have defined this method for an object, the system will invoke it whenever it needs to convert that object to a string.

JavaScript allows object syntax to be used to refer to properties and methods of primitive datatypes, such as JavaScript strings. When you do this with JavaScript expressions such as "hello".toUpperCase(), what actually happens is that JavaScript creates a temporary object "wrapper" for the primitive value (a string in this case) so that the method can be invoked or the property accessed. This primitive value to object conversion is performed automatically by JavaScript whenever necessary, but if you ever need to perform it yourself, you can do so by passing the primitive value to the Object() constructor.

Usage

When an Object object is newly created, it has no properties defined. As described in Chapter 7, Objects, you can add a property definition to an object simply by assigning a value to the property. Furthermore, objects can be used as associative arrays. In some cases you will want to create an "empty" object of this sort for use in your programs. You do this with:

blank_obj =index.html new Object();

In other cases, however, you will want to use a number of objects that are all of a certain type, such as objects that represent complex numbers, for example. To do this, you should write a constructor function for your object "class" and initialize the properties of the object as desired. You may also want to define a prototype object for your constructor function that will provide methods for all objects of your new object type.

Example

Defining the toString() method, and also the less frequently used assign() and valueOf() methods of an object, is most efficiently done in a constructor method for your object type, or with the prototype object of your object.

// define a constructor for the Complex object type
function Complex(x,y) { this.x =index.html x; this.y = y; }
// Create a dummy object to force the prototype object to be created
new Complex(0,0);
// give all Complex objects a toString() method
Complex.prototype.toString =index.html 
    new Function("return '{' + this.x + ',' + this.y + '}';");
// Create an object of this new Complex type
c =index.html new Complex(2, 2);
// Convert the object to a string, implicitly invoking the
// toString() method, and display the string.
alert("c =index.html " + c);   // displays string "{2,2}"

See Also

"Array", "Boolean", "Function", "Function.prototype", "Number", "String", "Window", Chapter 7, Objects


Previous Home Next
Number.valueOf() Book Index Object.assign()

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell

file: /Techref/language/java/SCRIPT/definitive/refp_274.htm, 10KB, , updated: 2019/10/14 15:00, local time: 2024/11/30 13:00,
TOP NEW HELP FIND: 
18.191.87.157:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://techref.massmind.org/Techref/language/java/SCRIPT/definitive/refp_274.htm"> [Chapter 21] Reference: Object</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to massmind.org!

 

Welcome to techref.massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .