如何在调用函数时向函数发送变量?

时间:2017-12-08 18:11:56

标签: javascript jquery

如何在调用变量message时将其发送到函数callmessage

<script>
var message = 'hello and this is your message'

callmessage(); // how can i send the variable 'message' to the function when I call it

function callmessage() {
   console.log(the text sent when the function was called)
}
</script>

2 个答案:

答案 0 :(得分:2)

<script>
var message = 'hello and this is your message'

callmessage(message);

function callmessage(x) {
   console.log(x);
}
</script>

如果您刚刚进入javascript,我强烈建议您查看一些有助于您编写更好代码的内容:

答案 1 :(得分:1)

您将其作为参数发送给函数,如:

var message = 'hello and this is your message'
callmessage(message); // how can i send the message variable
function callmessage(themessage) {
  console.log(themessage)
}

高度建议阅读https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions