另一个九天航模社
存在于Terra之上的另一个九天航模社
  • Home
  • Nichijou
  • Nijigen

JS的某种传统的OOP实现方式

1/12/2014

0 Comments

 
       记不清楚是从哪本讲JS的书上看到的了,因为JS的OOP和其他语言有很大的不同。所以很多人不能理解JS的OOP模式,这里有一种方式可以模拟出公有私有方法。在nanami-project里面使用了这种方法构造每一个模块。

var  system = function (params) {
         var gameTitle = 'NanamiProject',//私有变量
               order = 0,
               ...
               executeScript = function () {...},//私有方法
               ....
               that = {
                     getStatus: function () {...}//公有方法
               }
          return that;//这句很重要
}

在使用的时候,用构造函数system()构造一个对象
var sys = system();
然后就能使用类似sys.getStatus()来调用公有方法了,而私有方法是无法访问的,因为返回的对象里根本没有。
0 Comments

    Author

    Write something about yourself. No need to be fancy, just an overview.

    Archives

    March 2014
    January 2014
    December 2013

    Categories

    All

    RSS Feed

Powered by Create your own unique website with customizable templates.