Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

javascript - How to pass the value 'undefined' to a function with multiple parameters?

I want to pass the value of 'undefined' on a multiple parameter function but without omitting the parameter.

What do I mean with "without omitting the parameter". I mean that we should not just omit the parm2 like this example:

function myFunction (parm1, parm2) {}
myFunction("abc");

This will indeed make parm2 undefined, but I am not allowed to do it this way because I will need to specify other parameters AFTER the omitted parameter, so the previous method won't work in the case I want to make parm1 undefined BUT also want to have other parameters after this one to hold a value.

I have tried solving the problem with:

myFunction( ,"abc"); //doesn't seem to work

Update:

and myFunction(undefined, "abc"); ? this reliably works now.

However, it is worth mentioning that:

Setting a variable to undefined is considered a bad practice, we should be using null instead.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

myFunction(undefined,"abc"); this way should work, what is the problem?

see here

Here is undefined documentation from mozilla, supported by all browsers


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...