javascript - Is there any way to add a new parameter to a function programmatically? -
Since I can set the number of expectations by calling a function its function.length Property, do I have to programmatically create the right number of parameters to include in that function at runtime? Example:
var xyz = function (a, b) {}; Var BCD = function (A, B, C, D, E, F) {}; // vararg example var doc = document, func_length = xyz.length; Doc.xyz = (function () {returns function (A, B, C, D, E) works for one / but other is not}}}. Call (doctor); / * With `only two parameters ', like' Return function (A, B)`, if this function is used for `xyz` (although in this case 5 works fine), and return function ( If the function is used for 'BCD' (which does not work with only five parameters) with A, B, C, D, E, F) `6 / / Xyz.apply (blank, arguments) Think about ... but which logic ..? : // Returning function (A, B, C, D, E) does not support work with more than five parameters, which will be mostly class - hence my question / I also know very well That I use an object or array instead of // using a parameter ./ * This is to include a user-defined function for use in my code, while * to do the 'other stuff' later For my f (And * Varargs, of course). * Because the coding is something like: doc.xyz = xyz is inflexible * / As you can see, I Do not know how to do this, or if this is also possible, the search bar has not given me any other question like this, otherwise I did not want ...
< Note: This answer is a product of misunderstanding, but can help future visitors to this site.
Another way:
Do you really want to add the parameter? Write the function will be enough in such a way:
function foo () {// no debate = Logic [0] || ""; // First logic or (if not defined) empty string B = logic [1] || ""; // second argument etc. C = argument [2] || ""; // third argument etc. Warning (a + b + c); } Foo ("hello", "world!");
The solution you want:
The easiest way:
This is what you asked But this is not as simple as the previous solution.
You change with the Meta function with all the parameters and a handler function .
(function () {// wrapper var foo_meta = function (a, b, c, d) {// local meta fu alerts (a + b + c + d); // Doo Code}; Window.Fu = Function (A, B) {// Global EF Returns FU_MTA (A, B, "", "");}; Window.redefine_foo = function () {// global foo-changer / / Rewrites foo window.foo = function (a, b, c) {return foo_meta (a, b, c, "");}}};}) (); // wrapper // some code foo ("a", "b"); Redefine_foo (); // Rev. Fu Fu ("A", "B", "C"); // Note that foo_meta is not defined here foo_meta == undefined; // It is safe in the cover :) This will warn "now" and then "ABC". For the meaning of the wrapper function, see the reference.
Reference:
Argument array:
Ripping code:
Comments
Post a Comment