Javascript functions: Difference between revisions
From wikinotes
No edit summary |
|||
(3 intermediate revisions by the same user not shown) | |||
Line 17: | Line 17: | ||
javascript functions cannot return more than one variable. | javascript functions cannot return more than one variable. | ||
</blockquote><!-- Return Values --> | </blockquote><!-- Return Values --> | ||
= First Class Functions = | |||
<blockquote> | |||
Functions can be assigned to variables,<br> | |||
passed as arguments,<br> | |||
returned from functions. | |||
<source lang="javascript"> | |||
var do_thing = function() { console.log("hello") } | |||
do_thing() | |||
</source> | |||
</blockquote><!-- First Class Functions --> | |||
= Anonymous Functions = | = Anonymous Functions = | ||
Line 29: | Line 41: | ||
= Lambda Functions = | = Lambda Functions = | ||
<blockquote> | |||
== Basics == | |||
<blockquote> | <blockquote> | ||
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions | https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions | ||
Line 37: | Line 51: | ||
() => console.log('hi') // lambda expression w/o args | () => console.log('hi') // lambda expression w/o args | ||
</source> | </source> | ||
</blockquote><!-- Basics --> | |||
== Currying == | |||
<blockquote> | |||
<syntaxhighlight lang="javascript"> | |||
const sum = a => b => { a + b }; | |||
sum(1)(2); // 3 | |||
const plus_one = sum(1); | |||
plus_one(2); // 3 | |||
</syntaxhighlight> | |||
</blockquote><!-- Currying --> | |||
== Partials == | |||
<blockquote> | |||
<syntaxhighlight lang="javascript"> | |||
</syntaxhighlight> | |||
</blockquote><!-- Partials --> | |||
</blockquote><!-- Lambda Functions --> | </blockquote><!-- Lambda Functions --> | ||
Latest revision as of 12:12, 5 February 2023
Functions
function say_hello() { console.log("hello") return true } say_hello()See Also javascript arguments
Return Values
javascript functions cannot return more than one variable.
First Class Functions
Functions can be assigned to variables,
passed as arguments,
returned from functions.var do_thing = function() { console.log("hello") } do_thing()
Anonymous Functions
Functions do not need to be named.
myButton.onclick = function() { alert('hello') }
Lambda Functions
Basics
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
Also referred to as arrow function expressions.
(a, b) => a + b + 100 // lambda expression w/ args () => console.log('hi') // lambda expression w/o argsCurrying
const sum = a => b => { a + b }; sum(1)(2); // 3 const plus_one = sum(1); plus_one(2); // 3Partials
Closures
Javascript supports functions defined inside functions.
function foo() { function bar() { console.log('baz') } bar() }
Binding this
bind()
lets you change the value ofthis
within a method.B = { x: 2 } class A { x = 1 constructor() { this.printb = this.printx.bind(B) } // this.printb uses value of 'x' from 'B' printx() { console.log(this.x); } // this.printx uses value of 'x' from 'A' } const a = new A().printx() a.printx() a.printb()