如何在调用变量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>
答案 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