简体   繁体   English

在Javascript中将函数应用于Null

[英]Applying a Function to Null in Javascript

Why does the following work: 为什么以下工作:

function sum(a,b) { return a + b; }
var result = sum.call(null,3,4);     // 7

Why is result defined? 为什么要定义结果? I am invoking sum as a method of null. 我将sum作为null方法调用。 But null is not an object and cannot have properties! 但是null不是对象,并且不能具有属性!

What is going on? 到底是怎么回事?

The first argument for Function.prototype.call is the context , which defines the this value for the execution context of the invoked function, nothing else. Function.prototype.call的第一个参数是context ,它定义了被调用函数的执行上下文的this值,仅此而已。

So basically, you're saying that this is referring to null (at least, in ES5 strict mode), but since you don't access this anyway, it makes no difference. 因此,基本上,您是说this是指null (至少在ES5严格模式下),但是由于您无论如何都不访问this ,因此没有区别。

In non-strict mode, this cannot be null , so it's replaced with the global object instead. 在非严格模式下, this值不能为null ,因此将其替换为全局对象

When you use .call or .apply with null or undefined , the default this (usually window ) is automatically used instead if not in strict mode. 当您使用.call.applynullundefined ,默认this (通常window )将自动代替如果不是在严格模式。

From MDN (emphasis mine): MDN (重点是我的):

thisArg
The value of this provided for the call to fun . this规定的呼叫fun Note that this may not be the actual value seen by the method: 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. 请注意,这可能不是该方法看到的实际值: 如果该方法是非严格模式代码中的函数,则nullundefined将被全局对象替换 ,并且原始值将被装箱。

If you are in strict mode, it actually will be null or undefined . 如果你在严格模式下,它实际上将是nullundefined

(function() {
    'use strict';

    return this;
}).call(null); // null
(function() {
    'use strict';

    return this;
})(); // undefined

Its not. 不是。 NULL in this case specifies to what object the this keyword is bound to. 在这种情况下,NULL指定this关键字绑定到哪个对象。 In your method, by setting it to NULL it will either not have a this variable binding or it will be bound to the function itself or the window. 在您的方法中,通过将其设置为NULL,它将不具有this变量绑定,或者将其绑定到函数本身或窗口。

Since you're not using any variables or functions that are accessed via this , then there's no need to use the call method ... you can just use sum(3,4) 由于您没有使用通过this访问的任何变量或函数,因此不需要使用call方法...您可以只使用sum(3,4)

As stated in, for example, MDN , the first argument is MDN中所述 ,第一个参数是

The value of this provided for the call to [the method]. 此值提供给[方法]的调用。 Note that this may not be the actual value seen by the method: 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将被全局对象替换,原始值将被装箱。

As the MDN explained. 如MDN所述。

thisArg thisArg

The value of this provided for the call to fun. 此值提供给有趣的调用。 Note that this may not be the actual value seen by the method: 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将被全局对象替换,原始值将被装箱。

Simply put it by a little code. 只需用一点代码就可以了。

<script >
        'use strict'
        function fun() {
            alert(this);
        }
        fun.call(null);

    </script>

In the strict mode ,this is null. 在严格模式下,此为null。 But in the not strict mode . 但是在不严格的模式下。 this is window or global object. 这是窗口或全局对象。

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

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