JavaScript手册 
目录
此参考中包含
的内容
轻松上手
简介
操作符
语句
核心
文档
窗口
表单
浏览器
事件和
事件句柄
LiveWire
数据库服务
进程管理服务
实用工具
全局函数
LiveConnect
的Java包
索引
版权
 
【目录】 【上一页】 【下一页】 【索引】

Object

Object is the primitive JavaScript object type. All JavaScript objects are descended from Object. That is, all JavaScript objects have the methods defined for Object.

Core object
实现版本Navigator 2.0: toString 方法
Navigator 3.0, LiveWire 1.0: 添加了 eval and valueOf 方法s; constructor 属性
Navigator 3.0: removed eval 方法

创建源

The Object constructor:

new Object();

参数

属性概览

constructorSpecifies the function that creates an object's prototype.
prototypeAllows the addition of properties to all objects.

方法概览

evalEvaluates a string of JavaScript code in the context of the specified object.
toStringReturns a string representing the specified object.
unwatchRemoves a watchpoint from a 属性源 the object.
valueOfReturns the primitive value of the specified object.
watchAdds a watchpoint to a 属性源 the object.

属性

constructor

Specifies the function that creates an object's prototype. Note that the value of this property is a reference to the function itself, not a string containing the function's name.

属性源Object
实现版本Navigator 3.0, LiveWire 1.0

描述

All objects inherit a constructor property from their prototype:

o = new Object // or o = {} in Navigator 4.0
o.constructor == Object
a = new Array // or a = [] in Navigator 4.0
a.constructor == Array
n = new Number(3)
n.constructor == Number
Even though you cannot construct most HTML objects, you can do comparisons. For example,

document.constructor == Document
document.form3.constructor == Form

示例

The following example creates a prototype, Tree, and an object of that type, theTree. The example then displays the constructor property for the object theTree.

function Tree(name) {
   this.name=name
}
theTree = new Tree("Redwood")
document.writeln("<B>theTree.constructor is</B> " +
   theTree.constructor + "<P>")
This example displays the following output:

theTree.constructor is function Tree(name) { this.name = name; }

prototype

Represents the prototype for this class. You can use the prototype to add properties or methods to all instances of a class. For more information, see Function.prototype.

属性源Object
实现版本Navigator 3.0

方法

eval

Evaluates a string of JavaScript code in the context of this object.

方法源Object
实现版本Navigator 3.0, LiveWire 1.0
Navigator 4.0, Netscape Server 3.0: removed as 方法源 objects; retained as global function.

语法

eval(string)

参数

stringAny string representing a JavaScript expression, statement, or sequence of statements. The expression can include variables and properties of existing objects.

描述

The argument of the eval method is a string. If the string represents an expression, eval evaluates the expression. If the argument represents one or more JavaScript statements, eval performs the statements. Do not call eval to evaluate an arithmetic expression; JavaScript evaluates arithmetic expressions automatically.

If you construct an arithmetic expression as a string, you can use eval to evaluate it at a later time. For example, suppose you have a variable x. You can postpone evaluation of an expression involving x by assigning the string value of the expression, say "3 * x + 2", to a variable, and then calling eval at a later point in your script.

eval is also a global function, not associated with any object.

NOTE: In Navigator 2.0, eval was a top-level function. In Navigator 3.0 eval was also a method of every object. The ECMA-262 standard for JavaScript made eval available only as a top-level function. For this reason, in Navigator 4.0, eval is once again a top-level function. In Navigator 4.02, obj.eval(str) is equivalent in all scopes to with(obj)eval(str), except of course that the latter is a statement, not an expression.

示例

示例 1. The following example creates breed as a property of the object myDog, and also as a variable. The first write statement uses eval('breed') without specifying an object; the string "breed" is evaluated without regard to any object, and the write method displays "Shepherd", which is the value of the breed variable. The second write statement uses myDog.eval('breed') which specifies the object myDog; the string "breed" is evaluated with regard to the myDog object, and the write method displays "Lab", which is the value of the breed property of the myDog object.

function Dog(name,breed,color) {
   this.name=name
   this.breed=breed
   this.color=color
}
myDog = new Dog("Gabby")
myDog.breed="Lab"
var breed='Shepherd'
document.write("<P>" + eval('breed'))
document.write("<BR>" + myDog.eval('breed'))
示例 2. The following example uses eval within a function that defines an object type, stone. The statement flint = new stone("x=42") creates the object flint with the properties x, y, z, and z2. The write statements display the values of these properties as 42, 43, 44, and 45, respectively.

function stone(str) {
   this.eval("this."+str)
   this.eval("this.y=43")
   this.z=44
   this["z2"] = 45
}
flint = new stone("x=42")
document.write("<BR>flint.x is " + flint.x)
document.write("<BR>flint.y is " + flint.y)
document.write("<BR>flint.z is " + flint.z)
document.write("<BR>flint.z2 is " + flint.z2)

参看

eval

toString

Returns a string representing the specified object.

方法源Object
实现版本Navigator 2.0

语法

toString()
toString(radix)

参数

radix(Optional) An integer between 2 and 16 specifying the base to use for representing numeric values.

安全性

Navigator 3.0: This method is tainted by default for the following objects: Button, Checkbox, FileUpload, Hidden, History, Link, Location, Password, Radio, Reset, Select, Submit, Text, and Textarea.

For information on data tainting, see "JavaScript Security".

描述

Every object has a toString method that is automatically called when it is to be represented as a text value or when an object is referred to in a string concatenation. For example, the following示例 require theDog to be represented as a string:

document.write(theDog)
document.write("The dog is " + theDog)
You can use toString within your own code to convert an object into a string, and you can create your own function to be called in place of the default toString method.

Built-in toString methods
Every object type has a built-in toString method, which JavaScript calls whenever it needs to convert an object to a string. If an object has no string value and no user-defined toString method, toString returns "[object type]", where type is the object type or the name of the constructor function that created the object. For example, if for an Image object named sealife defined as shown below, sealife.toString() returns [object Image].

<IMG NAME="sealife" SRC="images\seaotter.gif" ALIGN="left" VSPACE="10"> Some built-in classes have special definitions for their toString methods. See the描述s of this method for these objects:

Array, Boolean, Connection, database, DbPool, Function, Number

User-defined toString methods
You can create a function to be called in place of the default toString method. The toString method takes no arguments and should return a string. The toString method you create can be any value you want, but it will be most useful if it carries information about the object.

The following code defines the Dog object type and creates theDog, an object of type Dog:

function Dog(name,breed,color,sex) {
   this.name=name
   this.breed=breed
   this.color=color
   this.sex=sex
}
theDog = new Dog("Gabby","Lab","chocolate","girl") The following code creates dogToString, the function that will be used in place of the default toString method. This function generates a string containing each property, of the form "property = value;".

function dogToString() {
   var ret = "Dog " + this.name + " is ["
   for (var prop in this)
      ret += " " + prop + " is " + this[prop] + ";"
   return ret + "]"
}
The following code assigns the user-defined function to the object's toString method:

Dog.prototype.toString = dogToString With the preceding code in place, any time theDog is used in a string context, JavaScript automatically calls the dogToString function, which returns the following string:

Dog Gabby is [ name is Gabby; breed is Lab; color is chocolate; sex is girl; toString is function dogToString() { var ret = "Object " + this.name + " is ["; for (var prop in this) { ret += " " + prop + " is " + this[prop] + ";"; } return ret + "]"; } ;]
An object's toString method is usually invoked by JavaScript, but you can invoke it yourself as follows:

alert(theDog.toString())

示例

示例 1: The location object. The following example prints the string equivalent of the current location.

document.write("location.toString() is " + location.toString() + "<BR>") The output is as follows:

location.toString() is file:///C|/TEMP/myprog.html 示例 2: Object with no string value. Assume you have an Image object named sealife defined as follows:

<IMG NAME="sealife" SRC="images\seaotter.gif" ALIGN="left" VSPACE="10"> Because the Image object itself has no special toString method, sealife.toString() returns the following:

[object Image] 示例 3: The radix parameter. The following example prints the string equivalents of the numbers 0 through 9 in decimal and binary.

for (x = 0; x < 10; x++) {
   document.write("Decimal: ", x.toString(10), " Binary: ",
      x.toString(2), "<BR>")
}
The preceding example produces the following output:

Decimal: 0 Binary: 0
Decimal: 1 Binary: 1
Decimal: 2 Binary: 10
Decimal: 3 Binary: 11
Decimal: 4 Binary: 100
Decimal: 5 Binary: 101
Decimal: 6 Binary: 110
Decimal: 7 Binary: 111
Decimal: 8 Binary: 1000
Decimal: 9 Binary: 1001

参看

Object.valueOf

unwatch

Removes a watchpoint set with the watch method.

方法源Object
实现版本Navigator 4.0, Netscape Server 3.0

语法

unwatch(prop)

参数

propThe name of a 属性源 the object.

描述

The JavaScript debugger has functionality similar to that provided by this method, as well as other debugging options. For information on the debugger, see Getting Started with Netscape JavaScript Debugger.

示例

See watch.

valueOf

Returns the primitive value of the specified object.

方法源Object
实现版本Navigator 3.0

语法

valueOf()

参数

描述

Every object has a valueOf method that is automatically called when it is to be represented as a primitive value. If an object has no primitive value, valueOf returns the object itself.

You can use valueOf within your own code to convert an object into a primitive value, and you can create your own function to be called in place of the default valueOf method.

Every object type has a built-in valueOf method, which JavaScript calls whenever it needs to convert an object to a primitive value.

You rarely need to invoke the valueOf method yourself. JavaScript automatically invokes it when encountering an object where a primitive value is expected.

表 4.2 shows the object types for which the valueOf method is most useful. Most other objects have no primitive value.

表 4.2 Use valueOf for these object types
对象 type Value returned by valueOf
NumberPrimitive numeric value associated with the object.
BooleanPrimitive boolean value associated with the object.
StringString associated with the object.
FunctionFunction reference associated with the object. For example, typeof funObj returns "object", but typeof funObj.valueOf() returns "function".

You can create a function to be called in place of the default valueOf method. Your function must take no arguments.

Suppose you have an object type myNumberType and you want to create a valueOf method for it. The following code assigns a user-defined function to the object's valueOf method:

myNumberType.prototype.valueOf = new Function(functionText) With the preceding code in place, any time an object of type myNumberType is used in a context where it is to be represented as a primitive value, JavaScript automatically calls the function defined in the preceding code.

An object's valueOf method is usually invoked by JavaScript, but you can invoke it yourself as follows:

myNumber.valueOf()

NOTE: Objects in string contexts convert via the toString method, which is different from String objects converting to string primitives using valueOf. All string objects have a string conversion, if only "[object type]". But many objects do not convert to number, boolean, or function.

参看

parseInt, Object.toString

watch

Watches for a property to be assigned a value and runs a function when that occurs.

方法源Object
实现版本Navigator 4.0, Netscape Server 3.0

语法

watch(prop, handler)

参数

propThe name of a 属性源 the object.
handlerA function to call.

描述

Watches for assignment to a property named prop in this object, calling handler(prop, oldval, newval) whenever prop is set and storing the return value in that property. A watchpoint can filter (or nullify) the value assignment, by returning a modified newval (or oldval).

If you delete a property for which a watchpoint has been set, that watchpoint does not disappear. If you later recreate the property, the watchpoint is still in effect.

To remove a watchpoint, use the unwatch method.

The JavaScript debugger has functionality similar to that provided by this method, as well as other debugging options. For information on the debugger, see Getting Started with Netscape JavaScript Debugger.

示例

<script language="JavaScript1.2">
o = {p:1}
o.watch("p",
   function (id,oldval,newval) {
      document.writeln("o." + id + " changed from "
         + oldval + " to " + newval)
      return newval
   })
o.p = 2
o.p = 3
delete o.p
o.p = 4
o.unwatch('p')
o.p = 5
</script> This script displays the following:

o.p changed from 1 to 2
o.p changed from 2 to 3
o.p changed from 3 to 4


【目录】 【上一页】 【下一页】 【索引】

回页面顶部回页面顶部