You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/06-function-object/article.md
+12-12Lines changed: 12 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
2
2
# Function object, NFE
3
3
4
-
As we already know, functions in JavaScript are values.
4
+
As we already know, a function in JavaScript is a value.
5
5
6
6
Every value in JavaScript has a type. What type is a function?
7
7
@@ -12,7 +12,7 @@ A good way to imagine functions is as callable "action objects". We can not only
12
12
13
13
## The "name" property
14
14
15
-
Function objects contain a few useable properties.
15
+
Function objects contain some useable properties.
16
16
17
17
For instance, a function's name is accessible as the "name" property:
18
18
@@ -24,14 +24,14 @@ function sayHi() {
24
24
alert(sayHi.name); // sayHi
25
25
```
26
26
27
-
What's more funny, the name-assigning logic is smart. It also assigns the correct name to functions that are used in assignments:
27
+
What's kind of funny, the name-assigning logic is smart. It also assigns the correct name to a function even if it's created without one, and then immediately assigned:
28
28
29
29
```js run
30
30
letsayHi=function() {
31
31
alert("Hi");
32
-
}
32
+
};
33
33
34
-
alert(sayHi.name); // sayHi (works!)
34
+
alert(sayHi.name); // sayHi (there's a name!)
35
35
```
36
36
37
37
It also works if the assignment is done via a default value:
@@ -93,7 +93,7 @@ alert(many.length); // 2
93
93
94
94
Here we can see that rest parameters are not counted.
95
95
96
-
The `length` property is sometimes used for introspection in functions that operate on other functions.
96
+
The `length` property is sometimes used for [introspection](https://en.wikipedia.org/wiki/Type_introspection) in functions that operate on other functions.
97
97
98
98
For instance, in the code below the `ask` function accepts a `question` to ask and an arbitrary number of `handler` functions to call.
99
99
@@ -102,9 +102,9 @@ Once a user provides their answer, the function calls the handlers. We can pass
102
102
- A zero-argument function, which is only called when the user gives a positive answer.
103
103
- A function with arguments, which is called in either case and returns an answer.
104
104
105
-
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.
105
+
To call `handler` the right way, we examine the `handler.length` property.
106
106
107
-
To call `handlers` the right way, we examine the `length` property:
107
+
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:
108
108
109
109
```js run
110
110
functionask(question, ...handlers) {
@@ -153,7 +153,7 @@ alert( `Called ${sayHi.counter} times` ); // Called 2 times
153
153
```warn header="A property is not a variable"
154
154
A property assigned to a function like `sayHi.counter = 0` does *not* define a local variable `counter` inside it. In other words, a property `counter` and a variable `let counter` are two unrelated things.
155
155
156
-
We can treat a function as an object, store properties in it, but that has no effect on its execution. Variables never use function properties and vice versa. These are just parallel worlds.
156
+
We can treat a function as an object, store properties in it, but that has no effect on its execution. Variables are not function properties and vice versa. These are just parallel worlds.
157
157
```
158
158
159
159
Function properties can replace closures sometimes. For instance, we can rewrite the counter function example from the chapter <info:closure> to use a function property:
@@ -241,7 +241,7 @@ let sayHi = function *!*func*/!*(who) {
241
241
sayHi("John"); // Hello, John
242
242
```
243
243
244
-
There are two special things about the name `func`:
244
+
There are two special things about the name `func`, that are the reasons for it:
245
245
246
246
1. It allows the function to reference itself internally.
247
247
2. It is not visible outside of the function.
@@ -282,7 +282,7 @@ let sayHi = function(who) {
282
282
};
283
283
```
284
284
285
-
The problem with that code is that the value of `sayHi` may change. The function may go to another variable, and the code will start to give errors:
285
+
The problem with that code is that `sayHi` may change in the outer code. If the function gets assigned to another variable instead, the code will start to give errors:
286
286
287
287
```js run
288
288
letsayHi=function(who) {
@@ -340,7 +340,7 @@ Functions are objects.
340
340
341
341
Here we covered their properties:
342
342
343
-
-`name` -- the function name. Exists not only when given in the function definition, but also for assignments and object properties.
343
+
-`name` -- the function name. Usually taken from the function definition, but if there's none, JavaScript tries to guess it from the context (e.g. an assignment).
344
344
-`length` -- the number of arguments in the function definition. Rest parameters are not counted.
345
345
346
346
If the function is declared as a Function Expression (not in the main code flow), and it carries the name, then it is called a Named Function Expression. The name can be used inside to reference itself, for recursive calls or such.
Static methods are used for the functionality that belongs to the class "as a whole". It doesn't relate to a concrete class instance.
202
206
203
-
## Summary
207
+
For example, a method for comparison `Article.compare(article1, article2)` or a factory method `Article.createTodays()`.
204
208
205
-
Static methods are used for the functionality that doesn't relate to a concrete class instance, doesn't require an instance to exist, but rather belongs to the class as a whole, like `Article.compare` -- a generic method to compare two articles.
209
+
They are labeled by the word `static` in class declaration.
206
210
207
211
Static properties are used when we'd like to store class-level data, also not bound to an instance.
208
212
@@ -218,13 +222,13 @@ class MyClass {
218
222
}
219
223
```
220
224
221
-
That's technically the same as assigning to the class itself:
225
+
Technically, static declaration is the same as assigning to the class itself:
222
226
223
227
```js
224
228
MyClass.property=...
225
229
MyClass.method=...
226
230
```
227
231
228
-
Static properties are inherited.
232
+
Static properties and methods are inherited.
229
233
230
-
Technically, for`class B extends A` the prototype of the class `B` itself points to `A`: `B.[[Prototype]] = A`. So if a field is not found in `B`, the search continues in `A`.
234
+
For`class B extends A` the prototype of the class `B` itself points to `A`: `B.[[Prototype]] = A`. So if a field is not found in `B`, the search continues in `A`.
0 commit comments