`

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.onclick=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.onclick=multFun(5,10)

 

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

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

分享到:
评论

相关推荐

    理解Javascript Function与Object

    理解Javascript Function与Object 在JavaScript中,Function和Object是两个非常重要的概念,它们之间存在着紧密的关系。在这篇文章中,我们将深入探讨Function和Object的关系,了解它们之间的联系和区别。 ...

    node-red-contrib-function-npm:节点红色功能节点,具有从npm安装软件包的能力

    具有从npm安装和使用软件包的能力相容性节点红色版本&gt; 1.0安装从您的节点红色目录npm install node-red-contrib-function-npm用法function-npm节点的行为类似于普通功能节点,不同之处在于允许在块内的脚本内使用npm...

    postcss-color-function:PostCSS插件将W3C CSS颜色功能转换为更兼容CSS

    postcss颜色功能 插件可将CSS颜色功能从...var colorFunction = require ( "postcss-color-function" ) // css to be processed var css = fs . readFileSync ( "input.css" , "utf8" ) // process css // set pres

    前端开源库-gulp-function

    通过这种方式,`gulp-function` 提供了一种强大的方式来构建高度定制化的前端构建流程,允许开发者充分利用 JavaScript 的强大能力,实现更灵活、更高效的任务处理。 在实际开发中,`gulp-function` 可以结合其他 `...

    Ajax-rebuild-js-function.zip

    Ajax-rebuild-js-function.zip,使用javascript,ajax代表异步javascript和xml。它是多种web技术的集合,包括html、css、json、xml和javascript。它用于创建动态网页,其中网页的小部分在不重新加载网页的情况下更改。

    04-JavaScript基础-定时器.pptx

    JavaScript 基础 - 定时器 JavaScript 中的定时器是指可以在指定时间执行某个函数或代码的机制。它可以分为两种:间隔型和延时型。间隔型定时器可以使用 `setInterval` 函数来实现,而延时型定时器可以使用 `...

    es6-arrow-function, shorthand 箭头函数编译为 ES5..zip

    es6-arrow-function, shorthand 箭头函数编译为 ES5. es6-arrow-function 编译使用箭头函数编写的JavaScript以使用ES5-compatible函数语法。 例如:[1, 2, 3].map(n =&gt; n * 2);编译为:[1, 2, 3

    JavaScript函数-深入解析与使用指南(很详细)

    ### JavaScript函数-深入解析与使用指南 #### 一、JavaScript函数的基本原理 JavaScript函数是一种能够封装特定任务的代码块,可以被重复调用以执行这些任务。这些函数可以通过接收参数来处理输入数据,并且通常会...

    javascript-code-improver

    如:&lt;script language="JavaScript"&gt;var i=0,s="",k=0;function foo(){for(j=0;j;j++){for(i=0;i;i++){s="string1";k=Math.floor(Math.random()*10);}for(i=20;i&gt;9;i--){s="string2";k=i;}}}&lt;/script&gt;&lt;br&gt;经过工具...

    javascript Object与Function使用.docx

    ### JavaScript中的Object与Function #### 一、引言 随着JavaScript的发展与标准化,这门语言已经成为Web开发领域不可或缺的一部分。然而,在深入学习JavaScript的过程中,不少开发者对于语言内部的一些概念仍感...

    javascript 基础 GIF套图

    JavaScript-function-base函数基础.gif Javascript-operational-character运算符.gif JavaScript-process-statement流程控制.gif JavaScript-regular-expressions正则表达式.gif JavaScript-string-function字符串...

    Javascript-range-function

    function range ( start , stop ) { let res = [ ] ; res . unshift ( start ) ; for ( let i = start ; i &lt; stop ; i ++ ) { res . push ( start += 1 ) ; } return res ; } 现在,如果要生成2点之间的...

    javascript Function

    这在JavaScript中是非常有用的特性之一: ```javascript (function() { // 独立的作用域 })(); ``` 通过这种方式定义的函数形成了一个独立的作用域,其中的变量不会被外部访问到。 #### 五、作为选择器 在某些...

    JavaScript课件-03.pptx

    在JavaScript中,表达式与运算符是编程的基础,它们决定了代码如何计算和操作数据。下面将详细介绍这些知识点。 首先,表达式是JavaScript中的基本元素,它可以是常量、变量,也可以是运算的结果。例如,`100`、`...

    JavaScript-JavaScript语法集锦

    ### JavaScript 语法集锦知识点详解 #### 一、概述 JavaScript 是一种广泛应用于网页开发的脚本语言,它能够使网页具有动态交互功能。本文档将详细介绍一系列常用的 JavaScript 语法和方法,帮助开发者更好地理解和...

    JavaScript应用实例-1箭头函数和function的this对比.js

    JavaScript应用实例-1箭头函数和function的this对比.js

    前端项目-function-plot.zip

    function-plot是建立在d3.js之上的,因此了解d3的基本概念和API是使用function-plot的前提。 2. **function-plot安装与引入** 要在项目中使用function-plot,首先需要通过npm或CDN引入。对于npm用户,可以运行`npm...

    格式化-function与小括号间留空格。链式调用不换行.zip

    "格式化-function与小括号间留空格。链式调用不换行"这一主题主要涉及JavaScript编程语言中的代码规范,包括函数调用时的空格使用以及链式调用的样式规则。 首先,我们来探讨函数调用时的小括号前后的空格问题。在...

    serverless-aliyun-function-compute:无服务器阿里云功能计算插件–向无服务器框架添加阿里云功能支持

    阿里云功能计算无服务器插件此插件可在无服务器框架内启用Aliyun Function Compute支持。入门先决条件使用插件的Node.jsv8...─ package.json└── serverless.yml在您的服务中安装serverless-aliyun-function-compute

    is-es6-generator-function:检查给定值是否为`GeneratorFunction`

    is-es6-generator-function 检查给定值是否为GeneratorFunction安装 npm i is-es6-generator-function --savenpm test用法有关更多用例,请参阅 var isGeneratorFunction = require ( 'is-es6-generator-function' )...

Global site tag (gtag.js) - Google Analytics