(function(){
function Student(){
this.name='stu';
this.age='18';
}
window.Student=Student;
})();
var s=new Student();
console.dir(s);
1,这种形式算闭包吗 ?
2,自调用函数算定义在全局中还是在哪呢?
(function(){
function Student(){
this.name='stu';
this.age='18';
}
window.Student=Student;
})();
var s=new Student();
console.dir(s);
1,这种形式算闭包吗 ?
2,自调用函数算定义在全局中还是在哪呢?
词法作用域中使用的域,是变量在代码中声明的位置所决定的。嵌套的函数可以访问在其外部声明的变量。
function init() {
var name = "Mozilla"; // name is a local variable created by init
function displayName() { // displayName() is the inner function, a closure
alert (name); // displayName() uses variable declared in the parent function
}
displayName();
}
init();
建议系统地了解一下
13 回答12.8k 阅读
7 回答1.9k 阅读
3 回答1.1k 阅读✓ 已解决
2 回答1.2k 阅读✓ 已解决
6 回答895 阅读✓ 已解决
6 回答1.1k 阅读
2 回答1.3k 阅读✓ 已解决
1.
闭包参见下面例子:
2.自调用函数定义在全局函数还是哪里,如果不知道的话,可以代码测试一下,比如下面这种:
如果是在全局函数中,上面test就可以访问到了,结果是报错了。
所以,答案也有了,自调用函数定义在
()
模块中