javascript - How can I pass a new array to a function? - Stack Overflow

This is bad practice, I know. It's just for testing, but I can't seem to get it to work. Can

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
Add a ment  | 

1 Answer 1

Reset to default 10

It'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

相关推荐

  • javascript - How can I pass a new array to a function? - Stack Overflow

    This is bad practice, I know. It's just for testing, but I can't seem to get it to work. Can

    7天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信