Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Aphorismen
Applications
Business Economics & Admin.
My Computers
Cooking
Devices
Folders
Food
Hardware
Infos
Software Development
Sports
Operation Instructions
Todos
Test
Help
Glossary
Community portal
adaptions
Sidebar anpassen
Wiki RB4
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
JavaScript/JScript
(section)
Page
Discussion
English
Read
Edit
View history
Toolbox
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Function Definition=== Internally functions are objects ([http://www.permadi.com/tutorial/jsFunc/index.html good explanation]). There are multiple ways of defining a function. Die Definition von Funktionen kann überall im Quellcode erfolgen und muss nicht vor der Verwendung erfolgen. function <FunctionName>(<ParameterList>) // only declaration, no execution { <StatementList>; return <Value|Parameter>; } <FunctionName>(); // execution or var foo = function() // only declaration, assigning variable to unnamed function object { <StatementList>; return <Value|Parameter>; } foo(); // execution or var add=new Function("a", "b", "return a+b;"); // very special, not recommended Functions can be properties of an object. ====ASEF/IIFE==== Another way is to create and execute a function created with function expression at once ('''anonymous self-execting function ASEF''') ([http://markdalgleish.com/2011/03/self-executing-anonymous-functions/ good explanation]), also better named IIFE ('''Immediately Invoked Function Expression, IIFE''')) by adding the parantheses following the function declaration: (function() { ... })'''()'''; // function() { ... })() missing brackets give a syntax error because it is a // function declaration instead of a function expresssion, only the latter can be executed (function(aParameter) { alert(aParameter); })'''("an argument")'''; // ASEF with parameter, will alert "an argument" (function($){ alert($(window).jquery); })'''(jQuery)'''; // avoids conflict with another library defining the '''$''' global variable All variables and functions defined within the anonymous function aren’t available to the code outside of it, so there is no naming conflict. The parameters are somehow passed by value and are available even if functions within are called later: // will alert two times with value 2 var f = (function (p) { alert(p); var x = function () { alert(p); }; return { y: x }; })(2); f.y(); ====Arrow Function==== Arrow Functions can remove the need of function, return and {} when defining functions. They are one-liners, similar to Lambda Expressions in Java or Python. ====Async Funtion==== An async function is a function declared with the async keyword, and the '''await''' keyword is permitted within it. The async and await keywords enable asynchronous, promise-based behavior to be written in a cleaner style. Await expressions make promise-returning functions behave as though they're synchronous by suspending execution until the returned promise is fulfilled or rejected. E.g.: function resolveAfter2Seconds() { return new Promise(resolve => { setTimeout(() => { resolve('resolved'); }, 2000); }); } '''async''' function asyncCall() { console.log('calling'); const result = '''await''' resolveAfter2Seconds(); console.log(result); } asyncCall(); console.log("asyncCall() called"); prints > "calling" > "asyncCall() called" > "resolved"
Summary:
Please note that all contributions to Wiki RB4 may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Uwe Heuer Wiki New:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Toggle limited content width