Home > Web Front-end > JS Tutorial > body text

JS中的public和private对象,即static修饰符_javascript技巧

WBOY
Release: 2016-05-16 17:56:50
Original
1407 people have browsed it
复制代码 代码如下:

//重新封装document对象
var Console={
Write:function(msg){alert(msg);}
};
//Person对象
var Person={
_name:"zzl", //static public
_age:28,
PrintInfo:function(){Console.Write("name:"+Person._name+",age:"+this._age);} //public method ,this表示Person
};

// People类型(对象)
var People=(function()
{
var _name="zzl";//private
var _age=28;
return {//public
PrintInfo:function(){Console.Write("name:"+_name+",age:"+_age);}
}
}
());

Person.PrintInfo();//对象中的方法
People.PrintInfo();//方法对象中的公开子方法
Console.Write(Person._name);//对象中的属性
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!