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
@@ -122,7 +122,7 @@ As you can see from the above image, `new Foo()` has a higher priority than `new
122
122
123
123
124
124
```js
125
-
new (Foo.getName());
125
+
new (Foo.getName());
126
126
(newFoo()).getName();
127
127
```
128
128
@@ -146,7 +146,7 @@ var obj = {
146
146
};
147
147
obj.foo();
148
148
149
-
// In the above two situations, `this` only depends on the object before calling the function,
149
+
// In the above two situations, `this` only depends on the object before calling the function,
150
150
// and the second case has higher priority than the first case .
151
151
152
152
// the following situation has the highest priority,`this` will only be bound to c,
@@ -156,7 +156,7 @@ var c = new foo();
156
156
c.a=3;
157
157
console.log(c.a);
158
158
159
-
// finally, using `call、apply、bind` to change what `this` is bound to ,
159
+
// finally, using `call、apply、bind` to change what `this` is bound to ,
160
160
// is another situation whom's priority is only second to `new`
161
161
```
162
162
@@ -177,7 +177,7 @@ Actually , the arrow function does not have `this` , `this` in the above functio
177
177
#### instanceof
178
178
179
179
The `instanceof` operator can correctly judge the type of the object , bacause it’s internal mechanism is to find out if `prototype` of this type can be found in the prototype chain of the object
180
-
let’s try to implement it
180
+
let’s try to implement it
181
181
```js
182
182
functioninstanceof(left, right) {
183
183
// get the `prototype` of the type
@@ -214,7 +214,7 @@ function b() {
214
214
}
215
215
```
216
216
217
-
It’s known that function and variable hoisting is the real reason for the above outputs . The usual explanation for hoisting says that the declarations are ‘moved’ to the top of the code , there is nothing wrong with that and it’s easy for everyone to understand . But a more accurate explanation should be like this :
217
+
It’s known that function and variable hoisting is the real reason for the above outputs . The usual explanation for hoisting says that the declarations are ‘moved’ to the top of the code , there is nothing wrong with that and it’s easy for everyone to understand . But a more accurate explanation should be like this :
218
218
219
219
There would bo two stages when the execution environment is generated . The first stage is the stage of creation(to be specific , the step of generating variable objects ) , in which the JS interpreter would find out the variables and functions that need to be hoisted, and allocate memory for them in advance , then the functions would be deposited into memory entirely , but the variables would only be declared and assigned to `undefined`, therefore , we can use them in advance in the second stage (the code execution stage)
220
220
@@ -337,7 +337,7 @@ function MyPromise(fn) {
337
337
// execute asynchronously to guarantee the execution order
`Debounce` and `Throttle` are different in nature. `Debounce` is to turn multiple executions into the last execution, and `Throttle` is to turn multiple executions into execution at regular intervals.
507
+
508
+
```Js
509
+
// The first two parameters with debounce are the same function
510
+
// options: You can pass two properties
511
+
// trailing: Last time does not execute
512
+
// leading: First time does not execute
513
+
// The two properties cannot coexist, otherwise the function cannot be executed
514
+
_.throttle=function(func, wait, options) {
515
+
var context, args, result;
516
+
var timeout =null;
517
+
// Previous timestamp
518
+
var previous =0;
519
+
// Empty if options is not passed
520
+
if (!options) options = {};
521
+
// Timer callback function
522
+
varlater=function() {
523
+
// If you set leading, then set previous to zero
524
+
// The first if statement of the following function is used
525
+
previous =options.leading===false?0:_.now();
526
+
// The first is prevented memory leaks and the second is judged the following timers when setting timeout to null
527
+
timeout =null;
528
+
result =func.apply(context, args);
529
+
if (!timeout) context = args =null;
530
+
};
531
+
returnfunction() {
532
+
// Get current timestamp
533
+
var now =_.now();
534
+
// It must be true when it entering firstly
535
+
// If you do not need to execute the function firstly
536
+
// Set the last timestamp to current
537
+
// Then it will be greater than 0 when the remaining time is calculated next
538
+
if (!previous &&options.leading===false)
539
+
previous = now;
540
+
// Calculated remaining time
541
+
var remaining = wait - (now - previous);
542
+
context =this;
543
+
args =arguments;
544
+
// It will add wait time if the current call time greater than the last call time
545
+
// Or the user manually adjusted the time
546
+
// This condition will only be entered if it set trailing
547
+
// This condition will be entered firstly if it not set leading
548
+
// Another point, you may think that this condition will not be entered if you turn on the timer
549
+
// In fact, it will still enter because the timer delay is not accurate
550
+
// It is very likely that you set 2 seconds, but it needs 2.2 seconds to trigger, then this time will enter this condition
551
+
if (remaining <=0|| remaining > wait) {
552
+
// Clean up if there exist a timer otherwise it call twice callback
553
+
if (timeout) {
554
+
clearTimeout(timeout);
555
+
timeout =null;
556
+
}
557
+
previous = now;
558
+
result =func.apply(context, args);
559
+
if (!timeout) context = args =null;
560
+
} elseif (!timeout &&options.trailing!==false) {
561
+
// Judgment if timer and trailing are set
562
+
// Turn on a timer if not set
563
+
// And you can't set leading and trailing at the same time
0 commit comments