Pass an optional argument while using the Arguments Object

Is it possible to use the argument object and still pass in a flag (Boolean)?  I need to have the flexibility of processing multiple possible arrays passed in and at the same time have an option flag passed when necessary. Is that possible and if so, how? So for an example I'd like to have the following type of functionality (there will always be at least one array passed in):
_dtmInsertStaticTags(arr1)
_dtmInsertStaticTags(arr1,arr2)
_dtmInsertStaticTags(arr1,flag)
_dtmInsertStaticTags(arr1,arr2,arr3,flag)
_dtmInsertStaticTags(arr1,arr2,arr3,arrN)
_dtmInsertStaticTags(arr1,arr2,arr3,arrN,flag)
function _dtmInsertStaticTags() {


    var sTags = [];
    for (var index = 0; index < arguments.length; index++) {
        sTags = sTags.concat(arguments[index]);
    }

    //routine to inject detokenized src values into container iframe
    var iframeTag = document.createElement("iframe");
    iframeTag.setAttribute("width", "1");
    iframeTag.setAttribute("height", "1");
    iframeTag.style.display = "none";
    iframeTag.style.border = "none";

    for (var i = 0; i < sTags.length; i++) {
        var imgTag = document.createElement("img");
        imgTag.src = sTags[i];
        iframeTag.appendChild(imgTag);
    }

    if (window.addEventListener) {
        window.addEventListener("load", function () { document.body.appendChild(iframeTag); }, false);
    } else {
        window.attachEvent("onload", function () { document.body.appendChild(iframeTag); });
    }
};

Open in new window

894359Asked:
Who is Participating?
 
Jan LouwerensConnect With a Mentor Software EngineerCommented:
You could use the typeof operator to determine if each argument is an array or a boolean (the flag).

for (var index = 0; index < arguments.length; index++) 
{
   console.log(typeof arguments[index]);
}

Open in new window

0
 
Leonidas DosasConnect With a Mentor Commented:
Yes it is possible.Check this simple example.We have a a() function and via if statement we check the typeof last argument of this function.If it is Boolean then an executing code is applying.
function a(){
  
  if((typeof arguments[arguments.length-1]=='boolean')){
    console.log(arguments[arguments.length-1]);
}
}
a(3,4,true);

Open in new window

Second example:
function a(){ 
  
  if((typeof arguments[arguments.length-1]=='object')){
    console.log(arguments[arguments.length-1]);
}
}
a(3,4,[true,false,false]);

Open in new window

Third example
function a(){ 
  
  if((typeof arguments[arguments.length-1]==='object')){
    if((typeof arguments[arguments.length-1][0]==='boolean')){
    console.log(arguments[arguments.length-1]);
   }
  }
}
a(3,4,[true,false,false]);

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.