JavaScript函数参数查询

发布于 2025-02-10 05:43:31 字数 136 浏览 0 评论 0原文

JavaScript是否接受多个字符串作为函数参数?

function name(Haydon Lyson){
//code
} 

会接受吗?如果不是这样,我们如何将多个字符串作为函数参数传递。

Do javaScript accept multiple strings as a function parameter?

function name(Haydon Lyson){
//code
} 

will it be accepted? If not then how can we pass multiple strings as function parameter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧城空念 2025-02-17 05:43:31

您可以使用其余参数
reast Parament

function name(...params){
  // ...params return an array of parameters
  // like this [hadon ,dsjfl,sdfl];


}
       or


function name(name1,name2,name3,name4){
// you can use multiple params if you know params exact length
// if you don't length of params then 
// use above methods

You can use the rest parameter for that
rest parament

function name(...params){
  // ...params return an array of parameters
  // like this [hadon ,dsjfl,sdfl];


}
       or


function name(name1,name2,name3,name4){
// you can use multiple params if you know params exact length
// if you don't length of params then 
// use above methods
淡看悲欢离合 2025-02-17 05:43:31

您可以通过逗号将它们分开时将它们传递:

function name(haydon, lyson) {
   console.log(haydon, layson)
}
let haydon = 1
let lyson = 5

name(hayden, lyson)

You can just pass them in separating them by comma:

function name(haydon, lyson) {
   console.log(haydon, layson)
}
let haydon = 1
let lyson = 5

name(hayden, lyson)
高跟鞋的旋律 2025-02-17 05:43:31

将多个字符串作为函数参数可以完成两种方式之一。

首先是将字符串作为参数吸收并通过逗号将它们分开。

var haydon = "Haydon";
var lyson = "Lyson";

function name(haydon, lyson){
//code
} 

第二种方法是将对象传递到您的函数中,以便您能够将键值对操纵到您的功能中的字符串中,从而可以只有一个单个参数提供大量输入。

let names = {
    cersei: 'Lannister',
    arya: 'Stark',
    jon: 'Snow',
    brienne: 'Tarth',
    daenerys: 'Targaryen',
    theon: 'Greyjoy',
    jorah: 'Mormont',
    margaery: 'Tyrell',
    sandor: 'Clegane',
    samwell: 'Tarly',
    ramsay: 'Bolton',
    haydon: 'Lyson'
}

function name(names){
//code
} 

希望这有帮助!

Passing multiple strings as a function parameter can be done one of two ways.

The first is by taking in the strings as parameters and separating them by commas.

var haydon = "Haydon";
var lyson = "Lyson";

function name(haydon, lyson){
//code
} 

The second way is to pass in an object into your function so that you can have the ability to manipulate the key-value pairs into strings in your function, allowing for a massive amount of inputs with just one single parameter.

let names = {
    cersei: 'Lannister',
    arya: 'Stark',
    jon: 'Snow',
    brienne: 'Tarth',
    daenerys: 'Targaryen',
    theon: 'Greyjoy',
    jorah: 'Mormont',
    margaery: 'Tyrell',
    sandor: 'Clegane',
    samwell: 'Tarly',
    ramsay: 'Bolton',
    haydon: 'Lyson'
}

function name(names){
//code
} 

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文