var leadingThirtysevenList = list.bind(undefined, 37);
This is a code snippet I saw in the manual about what the bind() function does when it passes in the second parameter, in which it passes undefined as a parameter. I have also seen the practice of passing undefined parameters in some frameworks, but I have not been able to find a more complete and clear answer through online searches. I would like to ask teachers to give a detailed answer.
仅有的幸福2017-05-19 10:46:01
The code you provided, why undefined is passed in, may have nothing to do with my explanation below.
I will mainly talk about some other frameworks; for example, jquery will pass in undefined as a parameter and assign a null value.
That’s because of a bug in js, that is, undefined can be rewritten! ! , yes, you heard it right.
As for why it is dangerous for undefined to be rewritten, look at the following code:
(function (undefined) {
let a;
// 如果a没有值则为a赋值. 但因为undefined被重写了,所以这句根本执行不到。
if (a === undefined) {
a = 'b';
}
})(true);
高洛峰2017-05-19 10:46:01
Then what do you think should be done if you want to pass the second parameter and use the default value for the first parameter or it doesn’t matter?
This code is just to implement function curry calling
怪我咯2017-05-19 10:46:01
ECMAScript5.1 15.3.4.4 explains that when the first parameter passed in is null or undefined, the context of the function will point to window (global in node)
習慣沉默2017-05-19 10:46:01
The first one is used as a placeholder, but using null will have a better effect than undefined. Generally, I am accustomed to using null to occupy positions.