-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
supplement for Event Loop, HOC & Closure, links to course
- Loading branch information
Showing
3 changed files
with
112 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
5. The 2 Pillars Closures and Prototypal Inheritance/1 - 15.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
## Functions are Objects (callable objects) | ||
review: when we define functions, the compiler looks at our code lexically, it determines what variables are available in variable environment. and also add scope chain. | ||
|
||
```javascript | ||
const func = new Function('num', 'return num') // function constructor | ||
|
||
func(1) | ||
``` | ||
|
||
```javascript | ||
function woho() { | ||
console.log('woho') | ||
} | ||
woho.yell = 'ahh' | ||
------------- underneath the hood --------------- | ||
const specialObj = { | ||
name: 'woho', | ||
(): console.log('woho'), | ||
yell: 'ahh' | ||
} | ||
``` | ||
- name (optional). when use IIFE or function expression, no name. | ||
- piece of code invoking with brackets. | ||
- properties (call. apply...) | ||
|
||
## First Class Citizens | ||
- function can be assigned to variable or property of object. | ||
- pass function as argument into another function. | ||
- return function as value from other function. | ||
|
||
## High Order Function | ||
- a function that can take a function as an argument. | ||
- a function that returns another function. | ||
|
||
hard code data --> when invoke, pass data --> when invoke, not only data, but pass action(function). | ||
|
||
> been able to achieve with HOF is this ability to tell the function what to do during invocation, we are able have our code dry and more flexible. breaking things down into small functionalities. | ||
## Closures (lexical scoping) | ||
is simply that a combination of function and the lexical environment. | ||
|
||
Closures allow a function to access variables from an enclosing scope or environment even after it leaves the scope in which it was declared. | ||
|
||
imagine closure is a memory box. and lexically analyze child function. variables outside the child function will be keep around if the child function is using it. anything referenced by the child function will be put into the closure box. | ||
|
||
- Memory efficient | ||
```javascript | ||
function heavyDuty(index) { | ||
const bigArray = new Array(10000).fill('A') | ||
return bigArray[index] | ||
} | ||
|
||
function heavyDuty2() { | ||
const bigArray = new Array(10000).fill('A') | ||
return index => bigArray[index] | ||
} | ||
``` | ||
|
||
- Encapsulation | ||
```javascript | ||
var makeCounter = function() { | ||
var privateCounter = 0 | ||
|
||
function changeBy(val) { | ||
privateCounter += val | ||
} | ||
|
||
return { | ||
increment: function() { | ||
changeBy(1) | ||
}, | ||
decrement: function() { | ||
changeBy(-1) | ||
}, | ||
value: function() { | ||
return privateCounter | ||
} | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# Advanced-JavaScript-Concepts-notes | ||
make notes of course Advanced JavaScript Concept | ||
make notes of [Advanced JavaScript Concept](https://www.udemy.com/advanced-javascript-concepts/) |