为什么在构造函数中有some_func(...)!= some_func.call(this,...) [英] Why is some_func(…) != some_func.call(this, …) in a constructor

查看:418
本文介绍了为什么在构造函数中有some_func(...)!= some_func.call(this,...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是假设 some_function(...) some_function.call ,...)。这似乎并不适用于构造函数/对象构造上下文中的调用:

I always assumed that some_function(...) was exactly the same as some_function.call(this, ...). This seems not to hold true for calls in constructors / an object construction context:

function Class(members, parent) {
    function Ctor(value) {
        members.__init__.call(this, value);
        return this;
    };
    Ctor.prototype = members;
    Ctor.prototype.__proto__ = parent.prototype;
    return Ctor;
}

var Base = Class({
    __init__: function(value) {
        this.value = value;
    }
}, {});

var Child = Class({
    __init__: function(value) {
        // Base(value*2); ← WON'T WORK AS EXPECTED
        Base.call(this, value*2); // works just fine
    }
}, Base);

Child .__ init __ 使用对 Base.call(this,value)的显式调用。如果我不使用这个冗长的表达式,这个会命名在调用的全局对象( window 基础构造函数。使用use strict会抛出一个错误,因为在严格模式下没有全局对象。

In Child.__init__ it in necessary to use the explicit call to Base.call(this, value). If I don't use this lengthy expressing, this would name the global object (window in browsers) in the called base constructor. With "use strict" an error would be thrown as there is no global object in strict mode.

请解释为什么我必须使用 Func.call(this,...)在此示例中?

Can someone please explain why I have to use Func.call(this, ...) in this example?

测试使用Node.js v0.6.12和Opera 12.50。)

(Tested with Node.js v0.6.12 and Opera 12.50.)

推荐答案

使用 .call调用函数不同于使用()调用它。使用 .call ,您为第一个参数中的调用显式设置 this 的值。使用正常调用,值将隐式为全局对象或 undefined ,具体取决于是否启用严格模式不是。

Calling a function with .call is different from just invoking it with (). With .call you explicitly set the value of this for the call in the first argument. With normal invocation, the this value will implicitly be the global object or undefined, depending on whether strict mode is enabled or not.

func.call({}, 1); //Call the function func with `this` set to the object and pass 1 as the first argument

func(1); //Call the function func with 1 as the first argument. The value of this inside the function depends on whether strict mode is on or off.

请参阅。call


我总是假定some_function(...)与
some_function.call(this,...)完全相同。这似乎不适用于调用
在构造函数/对象构造上下文中:

I always assumed that some_function(...) was exactly the same as some_function.call(this, ...). This seems not to hold true for calls in constructors / an object construction context:

这不是真的,一样。你可能很困惑它调用一个函数作为一个对象的属性。 obj.method()将意味着 obj 的值用于方法调用,它将与 obj.method.call(obj)有效地相同。

That's not true, they are never the same. You are probably confusing it with calling a function as a property of some object. obj.method() will mean that obj is the value of this for the method call and it would be effectively the same as obj.method.call(obj).

这篇关于为什么在构造函数中有some_func(...)!= some_func.call(this,...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆