1. Trang chủ >
  2. Công Nghệ Thông Tin >
  3. Kỹ thuật lập trình >

The length Property Functions as Data

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (3.5 MB, 461 trang )


var actual = args.length; The actual number of arguments
var expected = args.callee.length; The expected number of
n if they dont throw new ErrorWrong number of arguments: expected: +
expected + ; actually passed + actual; }
} Check that the actual of args matches the expected of args
arguments if actual = expected { Throw an exceptio
match
function fx, y, z { Throw an exception if they dont match
checkarguments; Now do the rest of the function normally
return x + y + z; }
The
length
property of the Function object is standardized by ECMAScript v1 and implemented in JavaScript 1.1 and later.
[3]
0,
[3]
In Netscape 4. a bug prevents this property from working correctly unless the
language
attribute of the
script
tag is

7.5.2 The prototype Property


Every function has a
prototype
property that refers to a predefined prototype object. pe object comes into play when the function is used as a constructor with the
it plays an important role in the process of defining new object types. Well erty in detail in
Chapter 8
explicitly set to JavaScript1.2.
This prototy
new
operator; explore this prop
.
lue persists across invocations, it is property of the Function object, instead of cluttering up the
global variable. For example, suppose we want to write a nction that returns a unique identifier whenever it is invoked. The function must never
unnecessary re is an example that returns a unique
integer whenever it is called:
Create and initialize the static variable. Function declarations are processed before code is executed, so

7.5.3 Defining Your Own Function Properties


When a function needs to use a variable whose va often convenient to use a
amespace by defining a n
fu return the same value twice. In order to manage this, the function needs to keep track of
the values it has already returned, and this information must persist across function invocations. We could store this information in a global variable, but that is
because the information is used only by the function itself. It is better to store the information in a property of the Function object. He
we really can do this assignment before the function declaration. uniqueInteger.counter = 0;
Heres the function. It returns a it is called and uses a static p
of the last value it returned. different value each time
roperty of itself to keep track function uniqueInteger {
Increment and return our static variable return uniqueInteger.counter++;
}

7.5.4 The apply and call Methods


ECMAScript v3 defines two methods that are defined for all functions,
call
and
apply
. These methods allow you to invoke a function as if it were a method of some other object. Note that we have not discussed methods yet; you may find this section
more understandable once you have read Chapter 8
. The first argument to both
call
and ction is to be invoked; this argument
becomes the value of the keyword within the body of the function. Any remaining
alues that are passed to the function that is invoked. For example, to pass two numbers to the function
f
and invoke it as if it were a method of
his is similar to the following lines of code:
.applyo, [1,2];
mbers, you could use the
apply
method to pass the elements of the array to the function:
[4]
apply
is the object on which the fun
this
arguments to
call
are the v the object
o
, you could use code like this:
f.callo, 1, 2;
T
o.m = f; o.m1,2;
delete o.m;
The
apply
method is like the
call
method, except that the arguments to be passed to the function are specified as an array:
f
For example, to find the largest number in an array of nu
Math.max
Script 1.2, but the
call
method is not
[4]
This example assumes we are using the ECMAScript v3
Math.max
function, which accepts an arbitrary number of arguments; the ECMAScript v1 version of the function accepts only two arguments.
var biggest = Math.max.applynull, array_of_numbers;
The
apply
method is implemented in Java l JavaScript 1.5
implemented unti

Xem Thêm
Tải bản đầy đủ (.pdf) (461 trang)

×