如何在屏幕上打印函数的所有参数
How to print on screen all the parameters of a function
我正在尝试将函数传递给 createTextNode
以便我在屏幕上打印:i am singing Let It GO
& i am dancing as well while singing
通过参数传递给函数。
问题是只出现了第一个参数。
function sing(song ,dance){
return`i am singing ${song}`
callback();
}
function dance(){
return "i am dancing as well while singing";
}
let singing=sing("Let It GO","dance");
const elP=document.createElement("p");
const elT=document.createTextNode(singing)
elP.appendChild(elT);
document.body.appendChild(elP);
Javascript 没有打印到屏幕的概念,这是一种粗略的调试方式,但有用于相同目的的控制台方法。
Javascript 有一个名为 arguments
的特殊关键字,它包含传递给函数的所有值。所以下面的代码应该打印所有传递给你的函数的参数:
function hello(a, b, c) {
console.log(arguments);
}
hello("World", 42, true);
虽然这种方法对我来说有点奇怪,但您要做的是将函数作为参数传递给 sing
函数,然后回调该参数:
function sing(song, callback){
return`i am singing ${song} ${callback()}`
}
function dance(){
return "i am dancing as well while singing";
}
let singing=sing("Let It GO", dance);
const elP=document.createElement("p");
const elT=document.createTextNode(singing)
elP.appendChild(elT);
document.body.appendChild(elP);
只需使用 string interpolation 引用 dance
,就像您引用 song
一样:
function sing(song ,dance){
return `i am singing ${song}, ${dance}`;
}
function dance(){
return "i am dancing as well while singing";
}
let singing=sing("Let It GO", dance());
const elP=document.createElement("p");
const elT=document.createTextNode(singing)
elP.appendChild(elT);
document.body.appendChild(elP);
但是,如果舞蹈函数仅 returns 一个字符串,更简洁的方法是在 sing()
参数本身中引用 dance
字符串,就像您对 song
字符串.
在下面我创建了一个名为 sing
的函数,就像你所做的一样,只是使用 ${song}
和 ${dance}
来检索参数,在函数括号本身内输入参数值并在我想要的任何地方使用它们可能是在控制台日志或div或警报框.
检查此 jsFiddle 或 运行 下面的 代码段 以查看以下代码的结果:
var div = document.getElementById("output");
function sing(song, dance) {
div.innerHTML = `<p>The song is: <strong>${song}</strong> and the dance is: <strong>${dance}</strong></p>`
}
sing("HipHop", "BreakDance");
<div id="output"></div>
您不需要在 sing() 中使用舞蹈作为参数
function dance(){
return "i am dancing as well while singing.";
}
function sing(song){
var c = dance();
return "i am singing " + song + "<br>" + c;
}
document.write(sing("foo"))
我正在尝试将函数传递给 createTextNode
以便我在屏幕上打印:i am singing Let It GO
& i am dancing as well while singing
通过参数传递给函数。
问题是只出现了第一个参数。
function sing(song ,dance){
return`i am singing ${song}`
callback();
}
function dance(){
return "i am dancing as well while singing";
}
let singing=sing("Let It GO","dance");
const elP=document.createElement("p");
const elT=document.createTextNode(singing)
elP.appendChild(elT);
document.body.appendChild(elP);
Javascript 没有打印到屏幕的概念,这是一种粗略的调试方式,但有用于相同目的的控制台方法。
Javascript 有一个名为 arguments
的特殊关键字,它包含传递给函数的所有值。所以下面的代码应该打印所有传递给你的函数的参数:
function hello(a, b, c) {
console.log(arguments);
}
hello("World", 42, true);
虽然这种方法对我来说有点奇怪,但您要做的是将函数作为参数传递给 sing
函数,然后回调该参数:
function sing(song, callback){
return`i am singing ${song} ${callback()}`
}
function dance(){
return "i am dancing as well while singing";
}
let singing=sing("Let It GO", dance);
const elP=document.createElement("p");
const elT=document.createTextNode(singing)
elP.appendChild(elT);
document.body.appendChild(elP);
只需使用 string interpolation 引用 dance
,就像您引用 song
一样:
function sing(song ,dance){
return `i am singing ${song}, ${dance}`;
}
function dance(){
return "i am dancing as well while singing";
}
let singing=sing("Let It GO", dance());
const elP=document.createElement("p");
const elT=document.createTextNode(singing)
elP.appendChild(elT);
document.body.appendChild(elP);
但是,如果舞蹈函数仅 returns 一个字符串,更简洁的方法是在 sing()
参数本身中引用 dance
字符串,就像您对 song
字符串.
在下面我创建了一个名为 sing
的函数,就像你所做的一样,只是使用 ${song}
和 ${dance}
来检索参数,在函数括号本身内输入参数值并在我想要的任何地方使用它们可能是在控制台日志或div或警报框.
检查此 jsFiddle 或 运行 下面的 代码段 以查看以下代码的结果:
var div = document.getElementById("output");
function sing(song, dance) {
div.innerHTML = `<p>The song is: <strong>${song}</strong> and the dance is: <strong>${dance}</strong></p>`
}
sing("HipHop", "BreakDance");
<div id="output"></div>
您不需要在 sing() 中使用舞蹈作为参数
function dance(){
return "i am dancing as well while singing.";
}
function sing(song){
var c = dance();
return "i am singing " + song + "<br>" + c;
}
document.write(sing("foo"))