javascript - Is it safe to pass 'arguments' to 'apply()' -


suppose have following code (completely useless, know)

function add( a, b, c, d ) {   alert(a+b+c+d); }  function proxy() {     add.apply(window, arguments); }  proxy(1,2,3,4); 

basically, know apply expects array second parameter, know arguments not proper array. code works expected, safe can pass array-like object second parameter in apply()?

the following work (in chrome @ least):

function proxy() {   add.apply(window, {     0: arguments[0],     1: arguments[1],     2: arguments[2],     3: arguments[3],     length: 4   }); } 

update: seems second code block fails in ie<9 while first 1 (passing arguments) works. error array or arguments object expected, shall conclude it's safe pass arguments, while it's not safe pass array-like object in oldie.

assuming ecmascript 5.1: yes. per ecma-262, 10.6, arguments object has length , index properties 15.3.4.3 (function.prototype.apply) requires.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -