Home >Web Front-end >JS Tutorial >Public and private objects in JS, namely static modifier_javascript skills

Public and private objects in JS, namely static modifier_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:56:501474browse
Copy code The code is as follows:

//Repackage the document object
var Console={
Write:function(msg){alert(msg);}
};
//Person object
var Person={
_name:"zzl", //static public
_age :28,
PrintInfo:function(){Console.Write("name:" Person._name ",age:" this._age);} //public method, this means Person
};

//People type (object)
var People=(function()
{
var _name="zzl";//private
var _age=28;
return { //public
PrintInfo:function(){Console.Write("name:" _name ",age:" _age);}
}
}
());

Person.PrintInfo();//Methods in the object
People.PrintInfo();//Public sub-methods in the method object
Console.Write(Person._name);//Attributes in the object
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn