, , :
var mystuff=function(){...}();
, -, .
:\
var mystuff=(function(){...})();
, . " " JavaScript. - , , , .
Encapsulation simply means that you make some members private. What is private in this case? Well, the blah variable is private in this case . Please learn how to start participating in private individuals, emphasizing that they are private. Good coding practice distinguishes public methods from private methods with underscores.
<script type="text/javascript">
var obj=(function(){
var _p={};
_p.list=[];
_p.init=function(){
_p.list=[];
};
return {
reset:function()
{
_p.init();
}
,addName:function(str)
{
_p.list.push(''+str);
}
,getNames:function()
{
return _p.list.slice(0);
}
,alertNames:function()
{
alert(this.getNames().join(","));
}
,getPrivateObj:function()
{
return _p;
}
};
})();
obj.alertNames();
obj.addName("Nandan");
obj.addName("Ram");
obj.alertNames();
obj.reset();
obj.alertNames();
obj.addName("Vinoth");
obj.addName("Kevin");
obj.alertNames();
alert(typeof obj._p);
var privateObj=obj.getPrivateObj();
alert(typeof privateObj);
alert(privateObj.list.join("|"));
</script>
source
share