简体   繁体   English

如何从函数调用中定义值

[英]how to define value from within the function call

I have this function call:我有这个函数调用:

<a href="javascript;;" onclick="helloWorld(value="planet")">

and then this function然后这个函数

function helloWorld(value) {
  return value
}

Obv, it doesn't work. Obv,它不起作用。 I need to be able to set the value of value from within the onclick .我需要能够在onclick中设置 value 的值。 Is this possible in js?这在js中可能吗?

The value that you are passing is known as arguments and the placeholder that is receiving the value is known as parameter .您传递的值称为arguments ,接收该值的占位符称为parameter

So whatever you pass arguments gets assigned to a placeholder (ie to parameter) in this case it is value因此,在这种情况下,无论您传递参数,都将分配给占位符(即参数),它是value

So you have defined a function who accept a parameter, so all you have to do is call it with single argument.所以你已经定义了一个接受参数的函数,所以你所要做的就是用单个参数调用它。

 function helloWorld(value) { return value }
 <a href="javascript;;" onclick="helloWorld('planet')"> link </a>

When you call your function with return to value it means result of helloWorld('planet') will be your value , in this case planet .当您调用return value的函数时,这意味着helloWorld('planet')结果将是您的value ,在本例中为planet When you pass argument to functions you just need to put in correct order, you do not need to define them with = .当您将参数传递给函数时,您只需要按正确的顺序放置,您不需要使用=定义它们。

Let's say you have function with 3 values like this假设你有这样的 3 个值的函数

function test(value1, value2, value3) {
   return value1+value2+value3
}

you need to just respect to order test(5,2,1) to identify each value您只需要遵守订单test(5,2,1)即可识别每个值

 function helloWorld(value) { console.log(value) //Here as you can see you already have acces to it return value //Also here when you call this function it return to value it means, result of helloWorld('anyString') will be your value }
 <a onclick=helloWorld("planet")> TEST </a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM