function
function
2022/07/26 (增加連結)
Basics
Javascript / function (吳濟聰老師web programming網站)
基礎
Creating the correct JS function for the job
Function declarations
Function expressions
Arrow function expressions
The essence
Three Ways to Define Functions in JavaScript
Function Declaration
Function Expression
Generator Function
[筆記] 進一步談JavaScript中函式的建立─function statements and function expressions
Organizing Your JavaScript Code with Functions
Defining Functions
Calling a Function
Part of a Function
Using Arguments
Pass in Arguments by Value
Pass in Arguments by Reference
Missing Arguments
Default Function Parameter Values
Calling Functions with More Arguments that Parameters
Variable Scope in Functions
Anonymous Functions
Recursion
Nesting Functions
Defining Function in an Object
Function declaration
Arguments & Parameters
Return
Scope
Hoisting
Different type of functions
The this keyword
10 Fundamentals You Need To Know About Functions in Javascript
完整/進階
First Order Functions
Immediately-Invoked Function Expressions
Function Closures
Functional Programming in JavaScript: Functions, Composition and Currying
Higher-Order Functions
Benefits of HOF
encapsulating behavior
cleaner code
Currying
Composition
When to use a function declaration vs. a function expression
The first difference: a name
Hoisting
The case for function expressions
IIFE
Callbacks
Use Function Expressions Instead of Function Declarations in JavaScript
Main difference: Hoisting
How memory management works in JavaScript
JavaScript memory management explained
Primitive types (string, number, boolean) are passed as a copy to function arguments.
Objects are passed as a reference to function arguments
Functions and arrays (and null, but it’s not really mutable as the two others) are considered as Objects by JavaScript
How references work in JavaScript and how to interpret it
JavaScript closures and references
Assignation vs Mutation with closure in JavaScript
What about React hooks?
Execution Context, Lexical Environment, and Closures in JavaScript
What is an Execution Context?
A Lexical Environment
Closures
Function in JavaScript Has More Secrets Than You Think
Pure Function
The Simplicity Of Writing Pure Functions : JavaScript, React, Redux
This Is How You Write Better Functions in JavaScript : Use the power of pure functions in JavaScript
Higher-Order Function
Function Caching
Lazy Function
Currying
Function Compose
5 Ways to Refactor If/Else Statements in JavaScript Functions
Default Parameters
OR Operator
Nullish Coalescing
Optional Chaining
no-else-returns and Guard Clauses
How JavaScript works: the different ways of declaring a function + 5 best practices
Callback
Callbacks: The Definitive Guide
Call back -> Promises & async/await
Call back -> Observables
Call back -> Generators
How to deal with nested callbacks and avoid “callback hell”
Write comments
Split functions into smaller functions
Using Promises
Using Async/await
How To Handle Errors in Asynchronous Javascript Code (when working with Callbacks)
3 Ways to Conditionally Invoke a Callback : The if statement, the And operator, and the optional chaining operator
Arrow functions (ES6)
arrow function是個在ES6之後的新語法,是較精簡的寫法,對新手而言,是個不容易理解的語法。
ES5 (unnamed function expression)
const times = function (a,b) {
return a * b;
}
console.log(times(1,2));
ES6: Arrow function ()
const times = (a,b) => {
return a*b
};
console.log(times(1,2));
省略大括號時,也省略return,計算結果就是回傳值。
const times = (a,b) => a*b;
console.log(times(1,2));
A Beginner’s Guide to Arrow Functions in ES6: Part 1
array.map() with arrow function
When (and why) you should use ES6 arrow functions — and when you shouldn’t
3 Examples of When Not to Use JavaScript Arrow Functions
Object Methods
Object Prototype
Dynamic Context
JavaScript Arrow Functions: How, Why, When (and WHEN NOT) to Use Them
JavaScript arrow functions are roughly the equivalent of lambda functions in python or blocks in Ruby.
When Not to Use Javascript Arrow Functions
Object Methods
Object Prototype
Callback functions with dynamic context
Makes the code less readable
How To Use Destructuring and Arrow Functions to Improve Your JavaScript Code
Arrow Functions vs Traditional Functions in JavaScript
Arrow functions get their this from their Lexical context they were declared in
JavaScript: What’s the Difference Between Normal and Arrow Functions?: Hint: ‘this’ is not the same
This Keyword
Arguments
Binds
Constructor
The Differences Between Arrow and Regular Functions You Should Know
this value
Constructors
arguments object
Implicit return
Methods
Arrow functions in JavaScript ES6
The dreaded “this” in JavaScript
“This” in the context of arrow functions
ES6 Arrow Functions, Scope, and what this means | JavaScript in the Wild
How to Accept Any Number of Arguments in a JavaScript Arrow Function
the ‘arguments’ object is not available to us when we are using arrow functions
Instead of an ‘arguments’ object, we can refer to a ‘rest’ parameter.
Arrow Functions vs. Regular Functions in JavaScript : How they work and which one to use
Arrow Functions vs. Regular Functions in JavaScript : Learn when and when not to use the arrow function
5 Simple Tips to Write Better Arrow Functions
Arrow function name inference
Inline when possible
Fat arrow and comparison operators
Constructing plain objects
Be aware of excessive nesting
Do you really know when to use an Arrow Function in JavaScript?
Differences between Arrow Function and Normal Function in JavaScript.
Shorter Syntax
The handling of the this keyword.
So, when to use which?
So, does it mean I don’t use Normal Function anymore?
How to return object literals from arrow functions in JavaScript
const actuallyReturnAnObjectArrowFunction = () => (
{hello: "world"}
)
Closure
A simple guide to help you understand closures in JavaScript
What is really so special about JavaScript Closures?
A closure is a function that has access to its ‘outer’ scope that it is defined.
Benefit of Closures
The first benefit of closures is to preserve local variables within the scope.
The second benefit, which is more of a general use case, is that it is useful in an asynchronous environment.
Understand How Javascript Closures Work with Simple Examples
Every function has reserved space in memory heap for closures.
Javascript engine will always make sure that functions have access to all of the variables outside the function.
When you’re processing an immense amount of data you can optimize memory usage.
When you’re working with sensitive data you can observe the principle of least privilege using encapsulation.
Closures can significantly improve the performance and security of your code.
A Beginner’s Guide to Understanding JavaScript Closures
a closure is just: A function inside another function that: 1) uses variables from its parent scope and 2) is exposed to the outside world.
JavaScript Closures : A brief and practical introduction to their functioning and use
JS Closures lets you create functions that access their enclosing function’s scope.
Javascript Closure: Simply Explained
In short, closure is a function with preserved data.
The closure means that an inner function always has access to the vars and parameters of its outer function, even after the outer function has returned Inner function can access variables and parameters of an outer function.
3 Use Cases for Closures (in JavaScript)
Tracking DOM State / Styles
Singletons
Higher-Order Functions
Understanding closures in JavaScript : What are closures, and what can we use them for?
Curry function
Functional Programming: Currying vs. Partial Application
What Is Currying?
What Is Partial Application?
Why I Can’t Explain What Currying Is
Functions and Values
First-Class Functions
Higher-Order Functions
Currying
Currying and Vanilla JavaScript: Examples of currying functions including DOM manipulation
A curried function is a function that takes multiple parameters one at a time: So, it takes a parameter, and returns a function that takes the next parameter, and so on until all parameters have been supplied, at which point, the application is completed and the final value is returned.
Introduction to currying in JavaScript
Currying implementations
Simple currying
General currying
Flexible currying
Limitations of the proposed implementations
Functional Fundamentals: Currying : A calm look at one of the more confusing concepts in functional programming (in F#)
Curried functions offer more flexibility. They let you build up the input parameters over time. You can give one input now. Then, later, give it another input. Then, finally, the last one right before it’s used.
5 Easy Steps To Master Currying and Higher-Order Functions in JavaScript
// in ES6
const curry = (fn) => {
return function curried(...args) {
const done = args.length >= fn.length
if (done) {
return fn.apply(this, args)
} else {
return (...args2) => curried.apply(this, [...args, ...args2])
}
}
}
Generator functions
The Basics of JavaScript Generators
Life Without Generator Functions
Generators: We Are Different!
Generators: Tell Me When I Can Start
Generators: Don’t Tell Me to Stop
Getting Values Back From Generators
Using the Return Keyword in Generators
Passing Values to the “Next” Function
Working With for … of Loops
Introduction to JavaScript generators: A beginner’s guide
Generators and Iterators
Syntax
yield pauses execution
yield vs return
yield: 2-way communication
Going async with generators
Generators are single-use
Infinite sequences
Use JavaScript Generator Functions to Reduce Memory Utilization: A comparison of memory management for ordinary JavaScript functions and generator functions
What is a JS generator? It is a function that can return a value and then continue the execution of the function at a later time. Whereas an ordinary JS function uses a return operator, the generator function uses a yield operator.
JavaScript: Understanding Generators
Iteration
Iterators
Iterables
Generators
How to use the Generator function* in JavaScript
a generator appears to be a function but it behaves like an iterator.
Generators in JavaScript: When should I use yield, and yield*?
Best of Modern JavaScript — Generator Best Practices and Regex
Explore Iterators and Generators in JavaScript : Get to know the applications of Iterators and Generators in JavaScript
How To Transform JavaScript Functions Into Memory-Efficient Generators
Use JavaScript Generator Functions to Reduce Memory Utilization: A comparison of memory management for ordinary JavaScript functions and generator functions
What is a JS generator? It is a function that can return a value and then continue the execution of the function at a later time. Whereas an ordinary JS function uses a return operator, the generator function uses a yield operator.
function *numberToN(N) {
for (let i = 0; i < N; i ++) {
yield i;
}
}
Higher order functions (ES6)
Understanding Higher-Order Functions in JavaScript
What is Functional Programming
First-Class Functions
Higher-Order Functions
Creating Our own Higher-Order Function
Higher-order functions in JavaScript
First-class functions
Higher-order functions
A higher-order function is a function that can take another function as an argument, or that returns a function as a result.
Higher Order Functions in JavaScript
First Class Functions
Taking Functions as Arguments
Returning Functions as Results
Functional Programming — Higher Order Function, HOF
In JavaScript, Functions are the First-Class Object
Then What is HOF?
The Real Power of HOF — Function Composition
Curry and Partial Application are an also HOF
5 Examples of Higher-Order Functions in JavaScript: Make your coding life easier with functional programming
.map, .forEach, etc.
Using Higher-Order Functions to Support Multiple Variations of an Operation
Reversing the Control
Composing
Internal State
7 Ways Higher Order Functions Can Help Improve Your Life
They Enhance Your Existing Code, Increasing Confidence
They Save Precious, Valuable Time
They Give The Ability To Create “Private Worlds”
They Can Be Used As Quick And Hacky Solutions, Temporarily Decreasing Pressure
It Can Hold, Manipulate, And Pass The Manipulated Data Anywhere, Giving You An Easy Time Testing Several Solutions
It Gives You The Ability To Create New Versions Of Something
It Can Help You Develop A Healthy Relationship With Your Boss And Your Co-workers
The Power of Higher-Order Functions in JavaScript
Higher-order functions are widely used in JavaScript and they exist in commonly used functions like .map, .filter, .reduce and .forEach.
Introduction to Higher-Order Functions: Understanding higher-order functions like map, filter, and reduce with examples
Never Write Another For-Loop in JavaScript: Seven higher-order functions to improve code readability
forEach()
reduce()
map()
filter()
find()
some()
every()
7 Useful JavaScript Higher-Order Functions that You Should Know
Learn How to Create Chainable Methods in Javascript With a Practical Example
5 Easy Steps To Master Currying and Higher-Order Functions in JavaScript
Writing High Order Functions for General Process Like a Senior JavaScript Developer : The power of functional programming
Hositing
Understanding Hoisting In Relation To Scope And Variables (JS)
4 Things You Should Know About JavaScript Hoisting
JavaScript Declarations are Hoisted
The let and const Keywords
JavaScript Initializations are Not Hoisted
Declare Your Variables At the Top
Immediately Invoked Function Express (IIFE)
Why it’s Time to Stop Using JavaScript IIFEs?
We Can Define Block-Scoped Variables in JavaScript
We Don’t Need Closures As Much Anymore
Aliasing Variables
Capturing the Global Object
Optimization for Minification
參考資料
JavaScript Functions Essential Knowledge
Function Expression
Function Hoisting
Arrow Function
Method Definition
Default Parameters
Default Function Parameter Values: JavaScript ES6 Feature Series (Pt 3)
Default Parameters In JavaScript And Their Pitfalls
If a value was not specified at all, use the default value
If that value was specified, but was undefined treat it as not specified
For all other cases use the passed in value exactly as sent
Rest Parameter
arguments Object
Setter and Getter
Generator Function
Function Declarations vs Function Expressions
Function Hoisting
Callback Pattern
Callbacks and Scope
Returning Functions
Lazy Function Definitions ( Self-Defining Functions )
Immediate Functions / Immediately-Invoked Functions (IIFEs)
Immediate Object Initialization
Function Properties — Memoization
Configuration Objects Pattern
JavaScript Functions (w3Schools)
JavaScript: A Review of Functions
Basic functions
Function expressions
Anonymous functions
Anonymous functions in JavaScript: What are anonymous functions and what are they good for?
Arrow functions
Factory functions
Immediately-invoked function expressions (IIFEs); the basics
Varieties of IIFEs
IIFEs in the wild
JavaScript Fundamentals: Mastering Functions
Defining functions
Function expressions
Calling functions
Scope
Nesting
Closures
The arguments object
Function parameters
Arrow functions
Using Default Parameters and the Rest Operator in JavaScript
DateTime.Now Should Be a Parameter: Write an idempotent function to calculate a person’s age
JavaScript Refactoring Tips — Making Functions Clearer and Cleaner
Using Destructuring for Object Parameters
Naming Our Callback Functions
Make Our Conditionals Descriptive
Replacing Switch Statements with Maps or Objects
Better Ways of Calling Things in JavaScript
Ways of Calling in JavaScript
The super Function
Prefer Arrow Functions as Callbacks
Using this as an Implicit Parameter
Prefer Function Declarations without Callbacks
Avoid IIFEs in ES6
Recursion
Three Important Rules for Recursions
The function should have a condition that self-destructs itself
The function must have a base case condition
Calls itself