Javascript 无法理解函数';行为
无法理解函数的行为Javascript 无法理解函数';行为,javascript,Javascript,无法理解函数的行为 function Animal() { console.log("showing an empty string: " + name); console.log("showing not defined: " + other); } Animal("Tommy"); 由于您的函数不接受任何参数,因此执行 console.log("showing an empty string: " + name); 将导致 showing an empty string
function Animal() {
console.log("showing an empty string: " + name);
console.log("showing not defined: " + other);
}
Animal("Tommy");
由于您的函数不接受任何参数,因此执行
console.log("showing an empty string: " + name);
将导致
showing an empty string:
执行时
console.log("showing not defined: " + other);
将导致错误“ReferenceError:未定义其他”
这种行为是因为您使用的是全局变量,并且每个窗口都定义了名称。默认情况下,它是“”(空字符串)
所以,如果您打开控制台并编写窗口.name
,您将得到“”
如果您编写
window.other
,您将得到未定义的,因为您的函数不接受任何参数,而不是执行
console.log("showing an empty string: " + name);
将导致
showing an empty string:
执行时
console.log("showing not defined: " + other);
将导致错误“ReferenceError:未定义其他”
这种行为是因为您使用的是全局变量,并且每个窗口都定义了名称。默认情况下,它是“”(空字符串)
所以,如果您打开控制台并编写窗口.name
,您将得到“”
如果你写window.other
你会得到未定义的,所以每个窗口
都有一个名称
属性,主要由窗口.name
访问
因此,当您调用该函数时,第一行是打印
showing an empty string:
因为name通常是一个空变量,而在第二行中变量other
没有定义,所以它抛出了错误。因此每个窗口
都有一个name
属性,主要可由窗口访问。name
因此,当您调用该函数时,第一行是打印
showing an empty string:
因为name通常是一个空变量,而在第二行中变量other
没有定义,所以它抛出了错误。它的行为如何?您希望它如何运行?您正在调用的函数
不是您定义的函数。。您定义的函数
不接受任何参数
。。因此,您将在控制台
中获得未定义错误
。请更具体地回答您的问题您不理解的是什么,您是否有任何错误,或者它是否未按预期工作?请更具体地说明您不理解的内容检查此项,如果您对此解释有任何疑问,请提出问题,然后将更具体地说明其行为如何?您希望它如何运行?您正在调用的函数
不是您定义的函数。。您定义的函数
不接受任何参数
。。因此,您将在控制台
中获得未定义错误
。请更具体地回答您的问题您不理解的是什么,您是否有任何错误,或者它是否未按预期工作?请更具体地说明您不理解的内容检查此项,如果您对此解释有任何疑问,请提出问题,然后将更具体地说明maheshv(y)。非常感谢你。贾斯蒂纳斯。明白:)。非常感谢。非常感谢你。贾斯蒂纳斯。明白:)。非常感谢