老师好,为什么后面调用方法也要写在立即执行函数里面?window.Person=Person不是已经把Person类写到了全局函数作用域中吗?
来源:3-5 私有属性和方法
城诗
2024-03-23 18:40:59
(function(){
let name='';
class Person{
constructor(username){
name=username;
}
getName(){
return name;
}
}
window.Person=Person;
})();
//下面为什么不能直接调用?
(function(){
const p=new Person('alex');
console.log(p.getName());
})()1回答
同学你好,注意听视频,讲师提到了“实际开发中,我们的代码也不会写在全局作用域中,大多会写在一个模块中”,所以如下立即执行函数,其实是模拟实际开发中,写在其他模块中的js代码:

祝学习愉快!
相似问题