【发布时间】:2016-12-17 18:21:25
【问题描述】:
我有这段代码,当我运行它时显示未定义。但是我们可以使用这个关键字访问全局属性。
var firstName = "Peter",
lastName = "Ally";
function showFullName () {
// "this" inside this function will have the value of the window object
// because the showFullName () function is defined in the global scope, just like the firstName and lastName
alert (this.firstName + " " + this.lastName);
}
showFullName ();
【问题讨论】:
-
jsbin.com/gerexi/1/edit?js,output — 我无法重现该问题。
-
我也不行
-
在严格模式下,它应该只是抛出一个错误。如果它被放置在 inside 另一个函数中,那么它会显示“undefined undefined”。
-
我越来越不确定。请检查小提琴。
-
@user1989488 在这种情况下,Fiddle 显然没有在全局范围内运行代码。
标签: javascript scope this global