w3resource
JavaScript Tutorial

Javascript object Object - Properties and Methods

Description

Parent of all JavaScript objects. it is a primitive javascript object type.

Can be created by Object constructor, for example, new object().

Javascript Objects Property

Name Description Version
constructor Specifies the function that creates an object's prototype. Implemented in JavaScript 1.2
prototype Use to add new properties and methods to an object. Implemented in JavaScript 1.2

Javascript Objects Methods

Name Description Version
tosource Use to get a string representation (source code) of the object. Implemented in JavaScript 1.2
tostring Rrepresent the source code of the specified object. Implemented in JavaScript 1.2
valueof Returns the primitive value of the specified object. Implemented in JavaScript 1.2
watch Use to watch a particular property of an object. Implemented in JavaScript 1.2
unwatch Use to remove a watch point for a particular property set by the watch() method. Implemented in JavaScript 1.2

JavaScript constructor Property: Object

Specifies the function that creates an object's prototype.

Syntax

Object.constructor

Object: The name of the object (Required).

Example:

The following web document demonstrates how the constructor property can be used.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript Object constructor Property :  Example-1</title>
</head>
<body><h1 style="color: red">JavaScript Object constructor Property</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
myarray = new Array("Orange", "Apple", "Banana", "Cherry", "Mango");
if(myarray.constructor == Array){
document.write("Object is created...");
}
//]]>
</script>
</body>
</html>

View the example in the browser

JavaScript prototype Property: Object

The prototype property is used to add new properties and methods to an object.

Syntax

object.prototype.property 
object.prototype.method 

Example:

The following web document demonstrates how the prototype property can be used.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript object prototype property :  Example-1</title>
</head>
<body><h1 style="color: red">JavaScript object prototype property</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
var mynum = new Number();
function square(x,y){
var numsqr;
numsqr = (x*x + 2*x*y + y*y);
return numsqr;
}
Number.prototype.sqrcal = square;
document.write("sqaure of (4+3) is  " +mynum.sqrcal(4,3) + "<br>");
//]]>
</script>
</body>
</html>

View the example in the browser

JavaScript toSource Method: Object

The toSource() method is used to get a string representation (source code) of the object.

Note that toSource for Object object is non-standard.

Syntax

toSource()

Parameters

None

Example:

In the following web document tosource() method returns the source code of the given object.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript Object object - toSource() method example</title>
</head>
<body>
<h1 style="color: red">JavaScript object : toSource() method</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
function student(name,class,rollno,sex)
{
this.name = name
this.class = class
this.rollno = rollno
tis.sex = sex
}
student1 = new student('Subhasish Chatterjee','VI',10,'M')
//Calling the toSource method of student1 displays the source which defines in the object.
document.write(student1.toSource());
//]]>
</script>
</body>
</html>

Supported Browser

Internet Explorer 7 Firefox 3.6 Google Chrome 7 Safari 5.0.1 Opera 10
Yes Yes Yes Yes Yes

JavaScript toString Method: Object

The toString() method returns a string, representing the source code of the specified object.

Syntax

Object.toString()

Parameters

None

Example:

In the following web document tostring() method returns the string representation of a given number.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript object - toString() method example</title>
</head>
<body>
<h1 style="color: red">JavaScript object : toString() method</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
<script type="text/javascript">
function Student(name,standard,rollno,sex) {
this.name=name;
this.standard=standard;
this.rollno=rollno;
this.sex=sex;
}
theStudent = new Student("Subhasish Chatterjee","VI",10,"Male");
document.write(theStudent.toString());
</script>
//]]>
</script>
</body>
</html>

View the example in the browser

Supported Browser

Internet Explorer 7 Firefox 3.6 Google Chrome 7 Safari 5.0.1 Opera 10
Yes Yes Yes Yes Yes

JavaScript valueOf Method: Object

The valueOf() method returns the primitive value of the specified object.

Syntax

valueOf() 

Parameters

None

Example:

In the following web document valueOf() method is used to get the primitive value of a number object.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript object - valueOf() method example</title>
</head>
<body>
<h1 style="color: red">JavaScript object : valueOf() method</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
var x = Number(201);
document.write(x.valueOf());
//]]>
</script>
</body>
</html>

View the example in the browser

Supported Browser

Internet Explorer 7 Firefox 3.6 Google Chrome 7 Safari 5.0.1 Opera 10
Yes Yes Yes Yes Yes

JavaScript watch() Method: Object

The watch() method is used to watch a particular property of an object. If the property is changed then watch() method turns on and runs a function.

Syntax

watch(prop, handler)

Parameters

prop: The name of a property of the object.

handler: A function to call.

Example:

The following web document demonstrates how the watch() method can be used.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript Object - watch () method example</title>
</head>
<body>
<h1 style="color: red">JavaScript Object : watch() method</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
o = {x:10}
o.watch("x",
function (id,oldvalue,newvalue) {
document.writeln("o." + id + " changed from " 
+ oldvalue + " to " + newvalue+"<br />")
return newvalue
})
o.x = 20
o.x = 30
o.x = 40
//]]>
</script>
</body>
</html>

View the example in the browser

Supported Browser

Internet Explorer 7 Firefox 3.6 Google Chrome 7 Safari 5.0.1 Opera 10
Yes Yes Yes Yes Yes

JavaScript unwatch() Method: Object

The unwatch() method of the object is used to remove a watchpoint for a particular property set by the watch() method.

Syntax

unwatch(prop)

Parameters

prop: The name of a property of the object.

Example:

The following web document demonstrates how the unwatch() method can be used.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript Object - unwatch () method example</title>
</head>
<body>
<h1 style="color: red">JavaScript Object : unwatch() method</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
a = {x:10}
a.watch("x",
function (id,oldvalue,newvalue) {
document.write("a." + id + " changed from " 
+ oldvalue + " to " + newvalue+"<br />")
return newvalue
})
a.x = 20
a.x = 30
a.x = 40
// start unwatch from here
a.unwatch('x')
a.x = 100
document.write("Current value of x is : "+a.x)
//]]>
</script>
</body>
</html>

View the example in the browser

>Supported Browser

Internet Explorer 7 Firefox 3.6 Google Chrome 7 Safari 5.0.1 Opera 10
Yes Yes Yes Yes Yes

See also:
JavaScript Core objects, methods, properties.



New Content: Composer: Dependency manager for PHP, R Programming