我正在观看有关 javascript 的 Douglas crockford 视频,他提到的一件事是,如果您忘记将new用于类,那么它将填充浏览器中的全局命名空间是 window。我想更好地理解这一点,所以我去测试了如下
var User = function(first,last){
this.name = first + " " + last;
}
var user = new User("John","Resig");
alert(window.name); // expected to see undefined, but was John Resig
alert(user.name); // this should only show John Resig correctly
var user1 = User("Douglas","Crockford");
alert(window.name); // expected to see Douglas Crockford and shown correctly
现在有两种可能,要么我对全局命名空间的理解是错误的,要么我的例子是错误的。感谢您是否可以让我朝着正确的方向前进。