简体   繁体   English

为什么Array.push应用和调用不起作用?

[英]Why Array.push apply and call not work?

I am don't understand why this code not work. 我不明白为什么这段代码行不通。 Here a.push.apply(this, b); 这里是a.push.apply(this,b); and a.push.call(window, 7); 和a.push.call(window,7); no works too. 也没有办法。

<script>
        var a = [1, 2, 3, 4];
        var b = [7];

        a["push"].apply(this, b);
</script>

You are "applying" push to the wrong object. 您正在“应用” push错误的对象。 Try: 尝试:

<script>
        var a = [1, 2, 3, 4];
        var b = [7];

        a["push"].apply(a, b);
</script>

Same reasoning for a.push.call(a, 7); a.push.call(a, 7);推理相同a.push.call(a, 7); .

a["push"] only gives you a function back with no information on the invocation object. a["push"]仅给您一个函数,而没有有关调用对象的信息。 When you call apply you are applying the function, what you need to do is to provide an "object context", that is the object on which you want to apply the function, and the function parameters. 调用apply时,您正在应用功能,您需要做的是提供一个“对象上下文”,即要在其上应用功能的对象以及功能参数。

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

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