Apply your JS skills to key Mozilla projects as an MDN Fellow! http://mzl.la/MDNFellowship

mozilla
검색 결과

    Function

    요약

    기본 개체

    JavaScript에 있는 모든 함수는 실제로 Function 개체입니다.

    Created by

    다른 모든 객체들처럼, Function 객체는 new 구문을 통해서 만들 수 있습니다. : 

    new Function ([arg1[,arg2[, ...argN]],]functionBody)
    
    arg1, arg2, ... arg N
    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.

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

    Description

    General

    Function objects created with the Function constructor are evaluated each time they are used. This is less efficient than declaring a function and calling it within your code, because declared functions are parsed only once.

    Specifying arguments with the Function constructor

    The following code creates a Function object that takes two arguments.

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

    The arguments "x" and "y" are formal argument names that are used in the function body, "return x * y".

    The preceding code assigns a function to the variable multiply. To call the Function object, you can specify the variable name as if it were a function, as shown in the following examples.

    var theAnswer = multiply(7, 6);
    
    var myAge = 50;
    if (myAge >= 39)
      myAge = multiply(myAge, .5);
    

    Properties

    arguments  : An array corresponding to the arguments passed to a function. This is deprecated as property of Function, use the arguments object available within the function instead.

    arity  : Specifies the number of arguments expected by the function. Use the length property instead.

    caller: Specifies the function that invoked the currently executing function (non-standard).

    constructor: Specifies the function that creates an object's prototype.

    length: Specifies the number of arguments expected by the function.

    name: The name of the function (not part of the standard).

    prototype: 함수를 이용해서 만든 모든 개체에 새로운 속성을 추가할 수 있게 합니다.

    Methods

    apply: Allows you to apply the method of another object in the context of a different object (the calling object).

    call: Allows you to call (execute) a method of another object in the context of a different object (the calling object).

    toSource: Returns a string representing the source code of the function. Overrides the Object.toSource method.

    toString: Returns a string representing the source code of the function. Overrides the Object.toString method.

    valueOf: Returns a string representing the source code of the function. Overrides the Object.valueOf method.

    Examples

    Example: Creating "focus" and "blur" event handlers for a frame

    The following example creates onFocus and onBlur event handlers for a frame. This code exists in the same file that contains the frameset tag. Note that scripting is the only way to create "focus" and "blur" event handlers for a frame, because you cannot specify the event handlers in the frame tag.

    var frame = frames[0];
    frame.onfocus = new Function("document.body.style.backgroundColor = 'white';");
    frame.onblur = new Function("document.body.style.backgroundColor = '#bbbbbb';");
    

    See also

    Functions

    문서 태그 및 공헌자

    Contributors to this page: HunminKim, Wafe, teoli
    최종 변경: teoli,