function Statement |
Declares a new function. See also Function Object
function name([argument1 [, argument2 [, ...argumentn]]]) { statements }
var name = function ( arguments ) stmt
var name = new Function ( arguments, stmt )Arrow functions added in ECMAScript 2015:
var name = ( args ) => { stmt }A "concise" body is assumed, where the expression is returned as if the body were { return expr }
var name = ( args ) => exprArrow functions combine nicely with array methods which accept a funtion. E.g.
var arr = [5, 6, 13, 0, 1, 18, 23];
var sum = arr.reduce((a, b) => a + b); //66
var even = arr.filter(v => v % 2 == 0); //[ 6, 0, 18]
var double = arr.map(v => v * 2); // [10, 12, 26, 0, 2, 36, 46]and can produce cleaner promise chains
promise.then(a => {
// ...
}).then(b => {
// ...
});They are the shortest way to provide an anonymous function:
setTimeout( () => { console.log('I happen sooner'); setTimeout( () => { // deeper code console.log('I happen later'); }, 1); }, 1);Note that arrow fuctions can be tricky:
- An arrow function does not have its own this; the this value of the enclosing lexical context is used. Rather nice for writing methods. No need to var me = this; ^
- Arrow functions do not have their own arguments object. Thus, arguments is simply a reference to the arguments of the enclosing scope
- To return an object literal, you must surround the body with () e.g.
let point = (x,y) => { x: x, y: y}
returns undefined because the {} makes the parser think it's the stmt form vs the expr form. This works:
let point = (x,y) => ({ x: x, y: y})Comma operator form (0, name()) This method of calling a function switches this from the local to the global this object. This may be useful because if you function.call(windows) you have to know the name of the global object. And depending on the environment, it might not be windows. E.g. Javascript running on a server, node, embedded, whatever. This pattern is supported in order to be able to process mutiple assignments in the for command: for (i=0, j=10; i < 10; i++,j--)
https://jsbin.com/kucutojono/edit?js,consolevar x = "xGlobalFromWindow"; var obj = { x: "xLocalFromObject", getMyX: function() { return this.x; } } console.log("(0, obj.getMyX)(): " + (0, obj.getMyX)() ); //"(0, obj.getMyX)(): xGlobalFromWindow" console.log("obj.getMyX(): " + obj.getMyX() ); //"obj.getMyX(): xLocalFromObject"The function statement syntax has the following parts:
Part Description name The name of the function. argument1...argumentn An optional, comma-separated list of arguments the function understands. statements One or more JScript statements.
Use the function statement to declare a function for later use. The code contained in statements is not executed until the function is called from elsewhere in the script.
© 1997 by Microsoft Corporation. All rights reserved.
See also:
file: /Techref/inet/iis/jscript/htm/js756.htm, 6KB, , updated: 2020/3/14 11:21, local time: 2024/12/4 12:03,
3.144.83.23:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://sxlist.com/TECHREF/inet/iis/jscript/htm/js756.htm"> function Statement</A> |
Did you find what you needed? |
Welcome to sxlist.com!sales, advertizing, & kind contributors just like you! Please don't rip/copy (here's why Copies of the site on CD are available at minimal cost. |
Welcome to sxlist.com! |
.