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.



Object.prototype.showValue = function () {
    alert(this.valueOf());
};
var str = "hello";
var iNum = 25;
str.showValue();
iNum.showValue();


Object()

Syntax



var variable = new Object(string)


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



object.eval(string)


Object.prototype

Syntax



object.prototype.property
    object.prototype.method


Object.toSource()

Syntax



object.toSource()


Object.toString()

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



<html>
    <body>
    <script language="JavaScript">
    <!--
    var aNum = Number(21);
    document.write(aNum.toString());
    -->
    </script>
    </body>
    </html>


Object.unwatch()

Syntax



object.unwatch(prop)


Object.valueOf()

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



<html>
    <body>
    <script language="JavaScript">
    <!--
    var age = Number(30);
    document.write(age.valueOf());
    -->
    </script>
    </body>
    </html>


Object.watch()

Syntax



object.watch(prop, function)


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:



object.hasOwnProperty(property)