Function

概述

在javascript中,一个函数实际上是一个Function对象.

语法

new Function ([arg1[, arg2

reference to undefined name 'mediawiki' Exception of type 'MindTouch.Deki.Script.Runtime.DekiScriptUndefinedNameException' was thrown. (click for details)
],] functionBody)

参数

arg1, arg2, ... argN
Names to be used by the function as formal argument names. Each must be a string that corresponds to a valid JavaScript identifier or a list of such strings separated with a comma; for example "x", "theValue", or "a,b".
functionBody
A string containing the JavaScript statements comprising the function definition.

描述

Function objects created with the Function constructor are parsed when the function is created. This is less efficient than declaring a function and calling it within your code, because functions declared with the function statement are parsed with the rest of the code.

All arguments passed to the function are treated as the names of the identifiers of the parameters in the function to be created, in the order in which they are passed. Functions created with the Function constructor do not create closures to their creation contexts; they always run in the window context.

Invoking the Function constructor as a function (without using the new operator) has the same effect as invoking it as a constructor.

例子

Creating functions with the Function constructor is one of the ways to dynamically create an indeterminate number of new objects with some executable code into the global scope from a function. The following example (a recursive shortcut to massively modify the DOM) is impossible without the invocation of the Function constructor for each new query if you want to avoid closures.

reference to undefined name 'syntax' Exception of type 'MindTouch.Deki.Script.Runtime.DekiScriptUndefinedNameException' was thrown. (click for details)

属性

For properties inherited by Function instances, see Properties of Function instances.

prototype
允许为每个Function对象进行扩展.
Properties inherited from Function:
arity, caller, constructor, length, name

方法

For methods inherited by Function instances, see Methods of Function instances.

Although the Function object does not provide any methods of its own, it does inherit methods through the prototype chain.

Methods inherited from Function:
apply, call, toSource, toString

Function 实例

Function实例继承于Function.prototype. As with all constructors, you can change the constructor's prototype object to make changes to all Function instances.

属性

prototype

方法

prototype

Example: 通过Function构造函数指定参数

下面的代码创建了一个包含有两个参数的Function对象.

reference to undefined name 'syntax' Exception of type 'MindTouch.Deki.Script.Runtime.DekiScriptUndefinedNameException' was thrown. (click for details)

函数体"return a + b"中的"a"和"b"是正式的参数名,.

浏览器兼容性

  • Desktop
  • Mobile

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support (Yes) (Yes) ? ? ?
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support ? (Yes) ? ? ?

Tags (0)

Edit tags
  • No tags

Attachments (0)

 

附加文件