先看下面的例子,它将告诉我们在JS世界中也有C#里的public , private ,及static等

//重新封装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);//对象中的属性

相关文章:

  • 2021-11-19
  • 2021-12-18
  • 2022-12-23
  • 2021-10-25
  • 2021-09-15
  • 2021-11-27
  • 2021-06-04
  • 2022-01-11
猜你喜欢
  • 2021-08-04
  • 2021-09-18
  • 2022-12-23
  • 2021-09-16
  • 2021-12-12
  • 2021-10-17
  • 2021-08-23
相关资源
相似解决方案