This commit is contained in:
Ilya Kantor 2019-07-31 23:18:20 +03:00
parent 4e9f302982
commit b6adf0b6aa
9 changed files with 51 additions and 106 deletions

View file

@ -1,7 +1,7 @@
# Function object, NFE
As we already know, functions in JavaScript are values.
As we already know, a function in JavaScript is a value.
Every value in JavaScript has a type. What type is a function?
@ -12,7 +12,7 @@ A good way to imagine functions is as callable "action objects". We can not only
## The "name" property
Function objects contain a few useable properties.
Function objects contain some useable properties.
For instance, a function's name is accessible as the "name" property:
@ -24,14 +24,14 @@ function sayHi() {
alert(sayHi.name); // sayHi
```
What's more funny, the name-assigning logic is smart. It also assigns the correct name to functions that are used in assignments:
What's kind of funny, the name-assigning logic is smart. It also assigns the correct name to a function even it's created without one, and then immediately assigned:
```js run
let sayHi = function() {
alert("Hi");
}
};
alert(sayHi.name); // sayHi (works!)
alert(sayHi.name); // sayHi (there's a name!)
```
It also works if the assignment is done via a default value:
@ -93,7 +93,7 @@ alert(many.length); // 2
Here we can see that rest parameters are not counted.
The `length` property is sometimes used for introspection in functions that operate on other functions.
The `length` property is sometimes used for [introspection](https://en.wikipedia.org/wiki/Type_introspection) in functions that operate on other functions.
For instance, in the code below the `ask` function accepts a `question` to ask and an arbitrary number of `handler` functions to call.
@ -102,9 +102,9 @@ Once a user provides their answer, the function calls the handlers. We can pass
- A zero-argument function, which is only called when the user gives a positive answer.
- A function with arguments, which is called in either case and returns an answer.
The idea is that we have a simple, no-arguments handler syntax for positive cases (most frequent variant), but are able to provide universal handlers as well.
To call `handler` the right way, we examine the `handler.length` property.
To call `handlers` the right way, we examine the `length` property:
The idea is that we have a simple, no-arguments handler syntax for positive cases (most frequent variant), but are able to support universal handlers as well:
```js run
function ask(question, ...handlers) {
@ -241,7 +241,7 @@ let sayHi = function *!*func*/!*(who) {
sayHi("John"); // Hello, John
```
There are two special things about the name `func`:
There are two special things about the name `func`, that are the reasons for it:
1. It allows the function to reference itself internally.
2. It is not visible outside of the function.
@ -347,6 +347,6 @@ If the function is declared as a Function Expression (not in the main code flow)
Also, functions may carry additional properties. Many well-known JavaScript libraries make great use of this feature.
They create a "main" function and attach many other "helper" functions to it. For instance, the [jquery](https://jquery.com) library creates a function named `$`. The [lodash](https://lodash.com) library creates a function `_`. And then adds `_.clone`, `_.keyBy` and other properties to (see the [docs](https://lodash.com/docs) when you want learn more about them). Actually, they do it to lessen their pollution of the global space, so that a single library gives only one global variable. That reduces the possibility of naming conflicts.
They create a "main" function and attach many other "helper" functions to it. For instance, the [jQuery](https://jquery.com) library creates a function named `$`. The [lodash](https://lodash.com) library creates a function `_`. And then adds `_.clone`, `_.keyBy` and other properties to (see the [docs](https://lodash.com/docs) when you want learn more about them). Actually, they do it to lessen their pollution of the global space, so that a single library gives only one global variable. That reduces the possibility of naming conflicts.
So, a function can do a useful job by itself and also carry a bunch of other functionality in properties.