简体   繁体   中英

Why Array.push apply and call not work?

I am don't understand why this code not work. Here a.push.apply(this, b); and 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. 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"] only gives you a function back with no information on the invocation object. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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