Created
November 29, 2019 18:35
-
-
Save lgg/8df2e2467fc57dd90fa0b1217954a83c to your computer and use it in GitHub Desktop.
JS Function arguments parsing visual explaination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//JS Function arguments parsing visual explaination | |
let i = { | |
p0: 0, | |
p1: 0, | |
p2: 0, | |
p3: 0, | |
pa: 0 | |
}; | |
const iText = [ | |
'[pass no arguments] ', | |
'[pass one argument] ', | |
'[pass many arguments] ' | |
] | |
function l(info, val){ | |
val = val ? val.toString() : val; | |
console.log('Command now: ' + info + ' \nvalue: ' + val); | |
} | |
function p0(){ | |
l('there is no parsed args in this function', '') | |
} | |
function p1(a){ | |
l(iText[i.p1] + 'parsed 1 argument:', a) | |
i.p1++; | |
} | |
function p2(a, b){ | |
l(iText[i.p2] + 'function parsed 2 arguments', '') | |
l('first arg', a) | |
l('second arg', b) | |
i.p2++; | |
} | |
function p3(a,b,c){ | |
l(iText[i.p3] + 'function parsed 3 arguments', '') | |
l('first arg', a) | |
l('second arg', b) | |
l('third arg', c) | |
i.p3++; | |
} | |
function pa(...allargs){ | |
l(iText[i.pa] + 'print all args with ', allargs) | |
i.pa++; | |
} | |
console.log('##################### Function parse no arguments') | |
p0(); | |
p0(1) | |
p0(1,2,3,4,5,6) | |
console.log('##################### Function parse 1 argument') | |
p1(); | |
p1(1) | |
p1(1,2,3,4,5,6) | |
console.log('##################### Function parse 2 arguments') | |
p2(); | |
p2(1) | |
p2(1,2,3,4,5,6) | |
console.log('##################### Function parse 3 arguments') | |
p3(); | |
p3(1) | |
p3(1,2,3,4,5,6) | |
console.log('##################### Function parse all arguments') | |
pa(); | |
pa(1) | |
pa(1,2,3,4,5,6) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment