Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

Specifying Arguments

As you already know, you can create an instance of the Function object that takes arguments. Take a look at the following statement:

var multiply = new Function("x", "y", "return x * y")

The last argument handed to the Function() constructor is the function’s body (in the form of a string). Preceding string arguments are formal parameter names that are used in the function’s body. Although they do not act as strings in the function, the arguments must be specified as strings. If you do not specify them as string literals, JavaScript attempts to evaluate them. If you use the following statement, for example, JavaScript will look for two variables, named x and y:

var multiply = new Function(x, y, "return x * y")

You can invoke a function reference created with the Function() constructor as if it were a declared function.

Using the Function Object with an Event Handler

You have already seen how to assign a function reference to an event handler. Here is a simple example:

<SCRIPT LANGUAGE="JavaScript">
<!--

function bye() {
  alert("Bye!")
}

window.onunload = bye

// -->
</SCRIPT>

We assign the window’s onunload event handler a function reference, not a function call. Note that you cannot use arguments with an event handler. When you want to assign a statement to an event handler with this technique, you must use a function reference, even if you want to specify only a single statement as demonstrated in the preceding script. It may seem very annoying to create a function for each event handler. Therefore, you can assign a function reference to an event handler via the Function() constructor. The preceding script would then be much more simple and organized:

<SCRIPT LANGUAGE="JavaScript">
<!--

window.onunload = new Function('alert("Bye!")')

// -->
</SCRIPT>

When the user unloads a document containing such a script, an alert dialog box pops up. When you assign a function reference to an event handler in this fashion, the event handler becomes a reference to a function object. When you use it as a function, the event handler method will be converted from an object to a function:

 window.onunload()

Properties of the Function Object

Instances of the Function object do not feature any properties. But, like all other dynamic objects in JavaScript (not in Navigator 2.0x or MSIE 3.0x), you can use its prototype property to extend the object by adding properties to it. Here is an interesting example:

function execute(x, y) {
 var now = new Date()
 if (now.getDay() == 5 && now.getDate() == 13) // Friday 13
  alert("This function does not execute on Friday the 13th")
 else
  this(x, y)
}

Function.prototype.exec = execute
var add = new Function("x", "y", "alert(x + y)")
add.exec(3, 4)

We use a function reference, execute, as a property (method) of Function.prototype, so the function execute() becomes a method of all instances of the Function object. Note that we could have used the Function() directly, instead of declaring a function and assigning its reference. Now, let’s take a look at the execute() function. It first creates an instance of the Date object reflecting the current time and date. We check if the current date happens to be Friday the 13th, and if so, a corresponding message is displayed via the window.alert() method. If it is not Friday the 13th, the statement this(x, y) executes. We shall refer to our specific instance, add, in order to make this clear. When we assign an instance of the Function object to the variable add, it becomes an object. You can invoke that object as a function:

add(3, 4)

You can also invoke its method, exec(), in the following fashion:

add.exec(3, 4)

When you call exec() as a method of the add object, its calling object is obviously add. Therefore, the keyword this refers to add, and this(x, y) is equivalent to add(x, y).

The Function object is quite extraordinary, because it is also a function. Figure 28-1 should make this object hierarchy very clear:


Figure 28-1.   A rounded rectangle represents a function or method, and a rectangle represents an object.

Summary

In this chapter, we dealt mostly with function references. We also discussed various evaluation methods and their behavior under specific conditions, including the MSIE 3.0x. We have discussed many advanced applications of the Function object, so you should have no problems using it. Bear in mind that all functions are objects, and parentheses are used only to invoke a function. As you will find out with practice, there are many things you can do with functions besides calling them.

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us