This is bad practice, I know. It's just for testing, but I can't seem to get it to work. Can I pass a new, initialized array to a JavaScript function?
I'm looking for the JavaScript equivalent of this C# method:
public void MyFunction(new string[]{"1","2","3"})
{
}
I tried this, but to no avail:
function MyFunction(new array ('1','2','3')) {
}
This is bad practice, I know. It's just for testing, but I can't seem to get it to work. Can I pass a new, initialized array to a JavaScript function?
I'm looking for the JavaScript equivalent of this C# method:
public void MyFunction(new string[]{"1","2","3"})
{
}
I tried this, but to no avail:
function MyFunction(new array ('1','2','3')) {
}
Share
Improve this question
edited Jul 24, 2012 at 20:59
katspaugh
17.9k12 gold badges67 silver badges105 bronze badges
asked Jul 24, 2012 at 20:43
YatrixYatrix
13.8k17 gold badges54 silver badges82 bronze badges
3
-
2
it's
new Array()
with a capital A. – JJJ Commented Jul 24, 2012 at 20:45 - 2 You (dmck) added JavaScript to the title? I thought we weren't supposed to do that since we use tags. – Yatrix Commented Jul 24, 2012 at 20:50
- 1 @katspaugh "Restore true justice." Hilarious, dude. – Yatrix Commented Jul 24, 2012 at 21:23
1 Answer
Reset to default 10It's easier than that:
function myFunction( arr ) { ... }
Then:
myFunction([1, 2, 3, "hello"]);
In function declarations there is no type information necessary (or possible). Just list out the parameter names. Then, when you call the function, you can use the array literal notation (just like you can in any expression to instantiate an array).
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744399522a4572315.html
评论列表(0条)