Javascript built in object arguments

  • 2020-03-30 02:23:17
  • OfStack

What is arguments
Arguments is a built-in object in JavaScript that is weird and often overlooked, but actually important. All the major js libraries make use of arguments objects. So the agruments objects are something that javascript programmers need to be familiar with.
All functions have their own arguments object that contains the arguments to be called. It is not an array; if typeof arguments is used, it returns 'object'. Arguments can be called using methods that call data, though. So length, index. But groups of push and pop objects are not appropriate.
Create a flexible function
It might seem like the argument object is pretty limited to use, but it's actually a very useful object. You can use the argument object to make it possible for the function to call an arbitrary number of arguments. Dean Edwards has a formatted function in his base2 library that shows this flexibility.

function format(string) {   
  var args = arguments;   
  var pattern = new RegExp( " %([1-" + arguments.length + "]) " ,  " g " );   
  return String(string).replace(pattern, function(match, index) {   
    return args[index];   
  });   
}; 

We provide a template string that you can use from "%1" to "%9" to add a placeholder to the return value. Then provide nine other parameter inserts.

format( " And the %1 want to know whose %2 you %3 " ,  " papers " ,  " shirt " ,  " wear " );

The code above will return: And the papers want to know whose shirt you wear".
One thing to note is that when we define a function, we only specify one parameter, string. Javascript allows us to pass any number of arguments to a function, however the function is defined. The Arguments object is allowed for both of these.
Converts the arguments object to a real array
The arguments object is not a real javascript array, but we can easily convert it to standard data and perform array operations.
var args = Array.prototype.slice.call(arguments); 

Now the variable args contains a standard javascript array object with all the arguments to the function.
Create a function through a preset arguments object
The Arguments object allows us to execute all types of javascript methods. Attach a definition of the makeFunc function. This function allows us to provide a function reference and all the parameters of the function. It will return an anonymous function to call the function you specify, and it will also provide the parameters that accompany the anonymous function call.
function makeFunc() {   
  var args = Array.prototype.slice.call(arguments);   
  var func = args.shift();   
  return function() {   
    return func.apply(null, args.concat(Array.prototype.slice.call(arguments)));   
  };   
}

The first argument object gives makeFunc a reference to the function you want to call. He is removed from the arguments array. MakeFunc then returns an anonymous function to run the specified method.
The argument of the first application points to the scope of the function call, mainly to the key parts of the function. So let's just keep this null. The second argument is an array that will be converted to an arguments object for this function. MakeFunc concatenates the original array values into the arguments object supplied to the anonymous function and the array of the called function.
You need to output a template always in the same position, so you don't have to always call the format function every time the template is referenced. You can use the generic functionality of makeFunc to return functions that can call format and then automatically supplement the template.

var majorTom = makeFunc(format,  " This is Major Tom to ground control. I'm %1. " );

You can call the majorTom function like this:

majorTom( " stepping through the door " );   
majorTom( " floating in a most peculiar way " );

Every time you call majorTom, it's going to call both the format function and the first argument, the template that's already written. So it will return
 " This is Major Tom to ground control. I'm stepping through the door. "    
 " This is Major Tom to ground control. I'm floating in a most peculiar way. " 

Create a function that references itself
You may think this is cool, but arguments has more surprises. He also has another useful feature: the callee method. Arguments. Callee includes a function reference to create an argument object. So how do you use it?
The arguments.callee method makes an anonymous function conveniently point to itself.
Repeat is a function that holds a function reference and two Numbers. The first number is the number of times the function is called, and the second number is the time between each call in milliseconds.
function repeat(fn, times, delay) {   
  return function() {   
    if(times �  > 0) {   
      fn.apply(null, arguments);   
      var args = Array.prototype.slice.call(arguments);   
      var self = arguments.callee;   
      setTimeout(function(){self.apply(null,args)}, delay);   
    }   
  };   
}

The Repeat function USES the arguments.callee method to get a reference from the variable self to the function that ran the original instruction. This way, the anonymous function can call itself again.
I have a super brief function that holds a string and executes the alert method.

function comms(s) {   
  alert(s);   
}  

However, I wanted to create a special version where I could repeat the action three times, every 2 seconds. Well, we can

var somethingWrong = repeat(comms, 3, 2000);   
somethingWrong( " Can you hear me, major tom? " ); 

The result of calling the somethingWrong function is to repeat the action three times, with an alert 2 seconds apart.
Arguments is not used very often and is a bit odd, but it is full of surprises and well worth learning about.


Related articles: