简体   繁体   English

fn.apply with first param null或undefined

[英]fn.apply with first param null or undefined

I am confused with fn.apply . 我对fn.apply很困惑。 For example, 例如,

Consider this snippet 考虑一下这个片段

Array.min = function(){
    return Math.min.apply( Math, arr );
}

I can grasp this. 我能理解这一点。 I understand fn.apply as, 我理解fn.apply为,

Calls a function with a given this value and arguments provided as an array 调用具有给定值的函数和作为数组提供的参数

But the below snippets also work 但下面的片段也有效

Array.min = function(){
    return Math.min.apply( null, arr );
}

Array.min = function(){
    return Math.min.apply( undefined, arr );
}

MDN explains this as, MDN将此解释为,

if the method is a function in non-strict mode code, null and undefined will be replaced with the global object, and primitive values will be boxed 如果该方法是非严格模式代码中的函数,则null和undefined 将替换为全局对象,并且原始值将被加框

I don't really understand the part which I have made bold. 我真的不明白我大胆的部分。 What is the meaning of that statement? 那句话是什么意思? Could anyone elaborate a bit? 任何人都可以详细说明一下吗?

The "global object" is the global object. “全局对象”是全局对象。 Just read the spec : 只需阅读规范

The unique global object is created before control enters any execution context. 在控制进入任何执行上下文之前创建唯一的全局对象。

In addition to the properties defined in this specification the global object may have additional host defined properties. 除了本规范中定义的属性外,全局对象还可能具有其他主机定义的属性。 This may include a property whose value is the global object itself; 这可能包括一个属性,其值是全局对象本身; for example, in the HTML document object model the window property of the global object is the global object itself. 例如,在HTML文档对象模型中,全局对象的window属性是全局对象本身。

In the browser, you're essentially calling Math.min.apply(window, arr) , which you can verify: 在浏览器中,您实际上是在调用Math.min.apply(window, arr) ,您可以验证:

(function() {
    console.log(this);  // Logs `Window {...}`
}).apply(undefined);

This is one of the many problems with non-strict mode. 这是非严格模式的许多问题之一。 this sometimes becomes window , and you silently end up creating global properties. this有时会变成window ,你默默地最终创建全局属性。 In strict mode, this will truly be undefined : 在严格模式下, this将是真正undefined

(function() {
    'use strict';

    (function() {
        console.log(this);  // Logs `undefined`
    }).apply(undefined);
})();

"Primitive values will be boxed" just says that primitives like numbers and strings will be "boxed" into a container object because primitives themselves are not objects. “原始值将被加框”只是说数字和字符串等基元将被“装箱”到容器对象中,因为基元本身不是对象。 Again, you can verify this: 同样,您可以验证这一点:

(function() {
    console.log(this);  // Logs `Number {}`, not `2`
}).apply(2);

Here's some more info on primitives: Why are JavaScript primitives not instanceof Object? 这里有一些关于基元的更多信息: 为什么JavaScript原语不是对象的实例?

if the method is a function in non-strict mode code, null and undefined will be replaced with the global object 如果该方法是非严格模式代码中的函数,则null和undefined将替换为全局对象

ie window in case of browsers, I am not sure what it refers to in case of nodejs, 即在浏览器的情况下窗口,我不知道在nodejs的情况下它是指什么,

and primitive values will be boxed 和原始值将被装箱

primitive values will be wrapped into their corresponding Objects, ie in case of fn.apply(2, args) the variable this inside fn will refer to a Number Object whose value will be 2. 原始值将在以下情况下被包装成其相应的对象,即fn.apply(2, args)的变量this内部fn将参考一些对象,其值将是2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM