JavaScript模块的封装方法,举例说明:比如怎样实现私有变量,不能直接赋值,只能通过共有方法访问。程序如下所示:
function MyObject(){ var privateVariable=10; function privateFunction(){ alert(privateVariable); } //特权方法; this.publicMethod=function(){ privateVariable++; return privateFunction; } } var obj=new MyObject(); obj.publicMethod()//11