JavaScript Tutorial/Object Oriented/Object

Материал из Web эксперт
Перейти к: навигация, поиск

Add method to Object class

Since all objects used in JavaScript are inherited from Object, all of the object can use the method added to Object.



   <source lang="javascript">

Object.prototype.showValue = function () {

   alert(this.valueOf());

}; var str = "hello"; var iNum = 25; str.showValue(); iNum.showValue();</source>


Object()

Syntax



   <source lang="javascript">

var variable = new Object(string)</source>


The Object() object is a primitive data type from which all JavaScript objects are derived.

Properties of the Object() Object Property Description constructor Function that creates an object eval() Evaluates a string of JavaScript code for the specified object prototype Prototypes new properties for the specific object toSource() Returns a string representation for the object toString() Converts the object to its string representation unwatch Removes a watchpoint for the object valueOf Returns the value of the specific object watch Adds a watchpoint to the object property

Object.eval()

Syntax



   <source lang="javascript">

object.eval(string)</source>


Object.prototype

Syntax



   <source lang="javascript">

object.prototype.property

   object.prototype.method</source>
   
  

Object.toSource()

Syntax



   <source lang="javascript">

object.toSource()</source>


Object.toString()

The toString() method gets a string representation of the Number object.



   <source lang="javascript">

<html>

   <body>
   <script language="JavaScript">
   
   </script>
   </body>
   </html></source>
   
  

Object.unwatch()

Syntax



   <source lang="javascript">

object.unwatch(prop)</source>


Object.valueOf()

The valueOf() method obtains the value of the specified object.



   <source lang="javascript">

<html>

   <body>
   <script language="JavaScript">
   
   </script>
   </body>
   </html></source>
   
  

Object.watch()

Syntax



   <source lang="javascript">

object.watch(prop, function)</source>


The Object Class

The Object class in JavaScript is similar to java.lang.Object in Java.

The Object class in JavaScript is the base class for all JavaScript classes.

All the properties and methods of the Object class are also present in the other classes.

The Object class has the following properties:

  1. constructor -- For the Object class, this points to the native Object() function.
  2. prototype -- For the all classes, this returns an instance of Object by default.

The Object class also has several methods:



   <source lang="javascript">

object.hasOwnProperty(property)</source>