The role of javascript function passing undefined as parameter
伊谢尔伦
伊谢尔伦 2017-05-19 10:44:01
0
4
658
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.

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(4)
仅有的幸福

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);
小葫芦

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

刘奇

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)

習慣沉默

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!