1 javascript function definitions

Upload: visamarinas6226

Post on 27-Feb-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/25/2019 1 JavaScript Function Definitions

    1/4

    JavaScript Function Definitions

    JavaScript functions are definedwith the functionkeyword.

    You can use a function declarationor a function expression.

    A function can be

    resionan

    ndeclaratioa

    exp

    Function Declarations

    Earlier in this tutorial, you learned that functions are declaredwith the following synta!

    function functionName"parameters# $ code to be executed

    %

    Declared functions are not eecuted i&&ediately. 'hey are (saved for later use(, and will be eecuted later, whenthey are invoked "called upon#.

    Ea&ple

    functionmyFunction(a, b) { returna * b;}

    Try it Yourself

    Se&icolons are used to separate eecutable JavaScript state&ents.Since a function declarationis not an eecutable state&ent, it is not co&&on to end it with a se&icolon.

    Function Epressions

    A JavaScript function can also be defined using an expression.

    A function epression can be stored in a variable!

    Ea&ple

    varx = function(a, b) {return a * b};

    Try it Yourself

    After a function epression has been stored in a variable, the variable can be used as a function!

    Ea&ple

    http://www.w3schools.com/js/tryit.asp?filename=tryjs_function_returnhttp://www.w3schools.com/js/tryit.asp?filename=tryjs_function_returnhttp://www.w3schools.com/js/tryit.asp?filename=tryjs_function_expressionhttp://www.w3schools.com/js/tryit.asp?filename=tryjs_function_expressionhttp://www.w3schools.com/js/tryit.asp?filename=tryjs_function_expressionhttp://www.w3schools.com/js/tryit.asp?filename=tryjs_function_return
  • 7/25/2019 1 JavaScript Function Definitions

    2/4

    varx = function(a, b) {return a * b};varz = x(4, 3);

    Try it Yourself

    'he function above is actually an anonymous function"a function without a na&e#.

    Functions stored in variables, do not need function na&es. 'hey are always invoked "called# using the variablena&e.

    'he function above ends with a se&icolon because it is a part of an eecutable state&ent.

    'he Function"# )onstructor

    As you have seen in the previous ea&ples, JavaScript functions are defined with the functionkeyword.

    Functions can also be defined with a built*in JavaScript function constructor called Function"#.

    Ea&ple

    varmyFunction = newFunction("a", "b", "return a * b");

    varx = myFunction(4, 3);

    You actually don+t have to use the function constructor. 'he ea&ple above is the sa&e as writing!

    Ea&ple

    varmyFunction = function(a, b) {return a * b};

    varx = myFunction(4, 3);

    Try it Yourself

    ost of the ti&e, you can avoid using the newkeyword in JavaScript.

    Function -oisting

    Earlier in this tutorial, you learned about (hoisting(.

    -oisting is JavaScript+s default behavior of &oving declarationsto the top of the current scope.

    -oisting applies to variable declarations and to function declarations.

    ecause of this, JavaScript functions can be called before they are declared!

    &yFunction"/#0

    function &yFunction"y# $ return y 1 y0%

    Functions defined using an epression are not hoisted.

    http://www.w3schools.com/js/tryit.asp?filename=tryjs_function_expression_variablehttp://www.w3schools.com/js/tryit.asp?filename=tryjs_function_expression_variablehttp://www.w3schools.com/js/tryit.asp?filename=tryjs_function_constructor2http://www.w3schools.com/js/tryit.asp?filename=tryjs_function_constructor2http://www.w3schools.com/js/tryit.asp?filename=tryjs_function_expression_variablehttp://www.w3schools.com/js/tryit.asp?filename=tryjs_function_constructor2
  • 7/25/2019 1 JavaScript Function Definitions

    3/4

    Self*2nvoking Functions

    Function epressions can be &ade (self*invoking(.

    A self*invoking epression is invoked "started# auto&atically, without being called.

    Function epressions will eecute auto&atically if the epression is followed by "#.

    You cannot self*invoke a function declaration.

    You have to add parentheses around the function to indicate that it is a function epression!

    Ea&ple

    (function() { varx = "Heo!!"; # wi invo$e my%ef})();

    'he function above is actually an anonymous self-invoking function"function without na&e#.

    Functions )an e 3sed as 4alues

    JavaScript functions can be used as values!

    Ea&ple

    functionmyFunction(a, b) { returna * b;}

    varx = myFunction(4, 3);

    JavaScript functions can be used in epressions!

    Ea&ple

    functionmyFunction(a, b) { returna * b;}

    varx = myFunction(4, 3) * &;

    Try it Yourself

    Functions are 5b6ects

    'he typeofoperator in JavaScript returns (function( for functions.

    ut, JavaScript functions can best be described as ob6ects.

    JavaScript functions have both propertiesand methods.

    'he argu&ents.length property returns the nu&ber of argu&ents received when the function was invoked!

    http://www.w3schools.com/js/tryit.asp?filename=tryjs_function_value2http://www.w3schools.com/js/tryit.asp?filename=tryjs_function_value2http://www.w3schools.com/js/tryit.asp?filename=tryjs_function_value2
  • 7/25/2019 1 JavaScript Function Definitions

    4/4

    Ea&ple

    functionmyFunction(a, b) { returnar'ument%en't;}

    'he toString()&ethod returns the function as a string!

    Ea&ple

    functionmyFunction(a, b) { returna * b;}

    vartxt = myFunctiontotrin'();

    A function defined as the property of an ob6ect, is called a &ethod to the ob6ect.A function designed to create new ob6ects, is called an ob6ect constructor.

    Previous

    http://www.w3schools.com/js/js_object_prototypes.asphttp://www.w3schools.com/js/js_object_prototypes.asp