JavaScript func_get_arg
Get the $arg_num'th argument that was passed to the function
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 30 | function func_get_arg (num) { // Get the $arg_num'th argument that was passed to the function // // version: 1008.1718 // discuss at: http://phpjs.org/functions/func_get_arg // + original by: Brett Zamir (http://brett-zamir.me) // % note 1: May not work in all JS implementations // * example 1: function tmp_a() {return func_get_arg(1);} // * example 1: tmp_a('a', 'b'); // * returns 1: 'a' if (!arguments.callee.caller) { try { throw new Error('Either you are using this in a browser which does not support the "caller" property or you are calling this from a global context'); //return false; } catch (e){ return false; } } if (num > arguments.callee.caller.arguments.length - 1) { try { throw new Error('Argument number is greater than the number of arguments actually passed'); //return false; } catch (e2){ return false; } } return arguments.callee.caller.arguments[num]; } |
Examples
Running
1 2 | function tmp_a() {return func_get_arg(1);} tmp_a('a', 'b'); |
Should return
1 | 'a' |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have func_get_arg goodness in JavaScript.
No comments yet. Be the first!
spread the word:
Use any PHP function in JavaScript
These kind folks have already donated: @HalfWinter, Paulo Freitas, Andros Peña Romo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>