javascript之--Function 与function

Assigning a function to a variable with the Function constructor. Suppose you create the variable multiply using the Function constructor, as shown in the preceding section:

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

 

This is similar to declaring the following function:

function multiply(x,y) {
   return x*y
} 

 

Assigning a function to a variable using the Function constructor is similar to declaring a function with the function statement, but they have differences:

  • When you assign a function to a variable using var multiply = new Function("..."), multiply is a variable for which the current value is a reference to the function created with new Function().

  • When you create a function using function multiply() {...}, multiply is not a variable, it is the name of a function.

Nesting functions. You can nest a function within a function. The nested (inner) function is private to its containing (outer) function:

  • The inner function can be accessed only from statements in the outer function.

  • The inner function can use the arguments and variables of the outer function. The outer function cannot use the arguments and variables of the inner function.

The following example shows nested functions: 

function addSquares (a,b) {
   function square(x) {
      return x*x
   }
   return square(a) + square(b)
}
a=addSquares(2,3) // returns 13
b=addSquares(3,4) // returns 25
c=addSquares(4,5) // returns 41 

When a function contains a nested function, you can call the outer function and specify arguments for both the outer and inner function: 

function outside(x) {
   function inside(y) {
      return x+y
   }
   return inside
}
result=outside(3)(5) // returns 8 

 

 

 

 

Specifying an event handler with a Function object.

The following code assigns a function to a window's onFocus event handler (the event handler must be spelled in all lowercase):

window.onfocus = new Function("document.bgColor='antiquewhite'") 

 

 

 

If a function is assigned to a variable, you can assign the variable to an event handler. The following code assigns a function to the variable setBGColor.

var setBGColor = new Function("document.bgColor='antiquewhite'")

You can use this variable to assign a function to an event handler in either of the following ways:

document.form1.colorButton.οnclick=setBGColor 

<INPUT NAME="colorButton" TYPE="button"
   VALUE="Change background color"
   onClick="setBGColor()"> 

 

 

 

Once you have a reference to a Function object, you can use it like a function and it will convert from an object to a function:

window.onfocus()

Event handlers do not take arguments, so you cannot declare any arguments in a Function constructor for an event handler. For example, you cannot call the function multiply by setting a button's onclick property as follows:

document.form1.button1.οnclick=multFun(5,10)

 

frames[0].onfocus = new Function("document.bgColor='antiquewhite'")

 解释:把一个Function给了一个事件,而这个Function定义时没有参数,只有方法体,这种情况是允许的,属于没有参数,只有方法体的方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值