Like SystemiConnectFri 15 Oct 2021, 04:50
Auto Save MessagesLGforumFri 26 Feb 2021, 13:31
New tutorial questionTheCrowMon 15 Feb 2021, 08:12
Support iOS Emojis (and other platforms)LGforumSun 14 Feb 2021, 01:25
LGforum (2806)
Allowing one parameter to contain more than one arguments Vote_lcapAllowing one parameter to contain more than one arguments Voting_barAllowing one parameter to contain more than one arguments Vote_rcap 
Mr.Easybb (1587)
Allowing one parameter to contain more than one arguments Vote_lcapAllowing one parameter to contain more than one arguments Voting_barAllowing one parameter to contain more than one arguments Vote_rcap 
Bloodbath (745)
Allowing one parameter to contain more than one arguments Vote_lcapAllowing one parameter to contain more than one arguments Voting_barAllowing one parameter to contain more than one arguments Vote_rcap 
Rukiafan (533)
Allowing one parameter to contain more than one arguments Vote_lcapAllowing one parameter to contain more than one arguments Voting_barAllowing one parameter to contain more than one arguments Vote_rcap 
Dom (513)
Allowing one parameter to contain more than one arguments Vote_lcapAllowing one parameter to contain more than one arguments Voting_barAllowing one parameter to contain more than one arguments Vote_rcap 
puppycheese (446)
Allowing one parameter to contain more than one arguments Vote_lcapAllowing one parameter to contain more than one arguments Voting_barAllowing one parameter to contain more than one arguments Vote_rcap 
pedro (330)
Allowing one parameter to contain more than one arguments Vote_lcapAllowing one parameter to contain more than one arguments Voting_barAllowing one parameter to contain more than one arguments Vote_rcap 
Neymar (301)
Allowing one parameter to contain more than one arguments Vote_lcapAllowing one parameter to contain more than one arguments Voting_barAllowing one parameter to contain more than one arguments Vote_rcap 
Hitsu (281)
Allowing one parameter to contain more than one arguments Vote_lcapAllowing one parameter to contain more than one arguments Voting_barAllowing one parameter to contain more than one arguments Vote_rcap 
Flora (275)
Allowing one parameter to contain more than one arguments Vote_lcapAllowing one parameter to contain more than one arguments Voting_barAllowing one parameter to contain more than one arguments Vote_rcap 


Allowing one parameter to contain more than one arguments

HitsuHitsu
Status : Finished on working fmAPI.

Posts : 281
Join date : 2013-09-09
Age : 25
Location : Indonesia
Sun 26 Apr 2015, 01:09
Ok, so I'm currently writing a little snippet to sort variable. It's not very useful though, so I'll only kept it as a snippet and were building on it to build more larger code than this.

Code:
function sortVar(variable) {
 arr = [];
 arr.push(variable);
}

What I want to ask is.. when I enter this :

Code:
var hitsu = "Hitsu";
var wasgreat = hitsu + " was great";
sortVar(hitsu, wasgreat);

It would only pushes 'Hitsu' to the arr variable, because there's only one parameter exist in the function.
So, what'd I want to ask is, how can I allow one parameter to contain more than one arguments? Like my case above.

Thanks for your answer.
BloodbathBloodbath
Status : No status yet...

Posts : 745
Join date : 2013-05-31
Age : 28
Sun 26 Apr 2015, 03:05
By default, all functions in JavaScript are so-called variadic functions.

This means that a function will run with any given number of arguments, less than, equal or greater than specified, all will run in the same matter.

If parameters are left out, the missing paramaters become [ic]undefined[/ic], and superfluous arguments are simply not able to be referenced by name.

Examples below.

Code:
function twoArguments(a, b) {
  console.log(a, b);
}
twoArguments(1); // this will log "1 undefined"
twoArguments(1, 2); // this will log "1 2"
twoArguments(1, 2, 3); // this will still log "1 2"

Now for the solution here, JavaScript provides an array-like object [ic]arguments[/ic] that is only defined within functions.

This special object contains all the arguments passed to the function in the order they were passed, and a length property determining how many arguments were passed to the function.

It also contains two properties that are very specific so I won't go into detail here, they're also deprecated so you shouldn't use them anyway.

So with this in mind, we can write code like this:

Code:
function logArguments() {
  for(var i = 0, len = arguments.length, c; (c = arguments[i++]); ) {
    console.log(c);
  }
}
logArguments(1); // logs 1
logArguments(1, 2); // logs 1, 2 on a new line
logArguments(1, 2, 3); // logs 1, 2 on a new line, 3 on a new line
logArguments(1, 2, 3, 4); // and so on

And in conclusion, we can write code like you want. This will simply push all arguments to your array and return it.

Code:
function pushArgs() {
  var arr = [];
  return (Array.prototype.forEach.call(arguments, function(v) {
    arr.push(v);
  }), arr);
}
pushArr("Hitsu", "was", "here"); // this will produce ["Hitsu", "was", "great"]

However, [ic]Array.prototype.push[/ic] will automatically implement your desired behaviour: if you pass more than one argument to it, it will add all of the arguments to the end of the array, in the order they were passed.

Code:
var arr = [];
arr.push("Hitsu", "was", "here");
arr; // will also be ["Hitsu", "was", "here"

I hope this makes things clear.