在JavaScript中创建Namespace / Class类型结构的最优雅方式

时间:2021-10-01 23:52:09

I am trying to settle on a method that will provide me the most elegant way of wrapping my code in Namespace/Unit like objects. For example Google Maps API's var a = Google.Maps.Foo();, which I think seems quite clean.

我试图找到一种方法,它将为我提供在Namespace / Unit中包装代码的最优雅方式。例如Google Maps API的var a = Google.Maps.Foo();,我认为这看起来很干净。

I'd like it to enclose (if that is the right term) the jQuery No Conflict $ sign as well.

我希望它包含jQuery No Conflict $符号(如果这是正确的术语)。

So far I am liking:

到目前为止我很喜欢:

// Top level container for sub objects
var myApp = myApp || {}; 

// An object to be held in myApp     
(function( skillet, $, undefined ) {

    //Private Property
    var isHot = true;

    //Public Property
    skillet.ingredient = "Bacon Strips";

    //Public Method
    skillet.fry = function() {
        var oliveOil;

        addItem( "\t\n Butter \n\t" );
        addItem( oliveOil );
        console.log( "Frying " + skillet.ingredient );

        return "Fried!";
    };

    //Private Method
    function addItem( item ) {
        if ( item !== undefined ) {
            console.log( "Adding " + $.trim(item) );
        }
    }   

}( window.myApp.skillet = window.myApp.skillet || {}, jQuery ));

Can anyone expand on this, point out potential problems, or just offer a better methodology in general?

任何人都可以扩展这一点,指出潜在的问题,或者只提供更好的方法吗?

1 个解决方案

#1


11  

Check this JavaScript Module Pattern and this Learning JavaScript Design Patterns

检查此JavaScript模块模式和此学习JavaScript设计模式

Module example:

模块示例:

var MyModule = (function($){
  var MY_CONSTANT = 123;

  var _myPrivateVariable = 'TEST MEH';
  var _$myPrivateJqueryObject = $('div.content');

  var _myPrivateMethod = function(){
    alert('I am private!');
  };

  var myPublicMethod = function(){
    console.log('Public much?');
  }

  return {
      myPublicMethod : myPublicMethod 
  };

})(jQuery);

MyModule.myPublicMethod();

Class example:

类示例:

function Person(name, age){
   this.name = name || '';
   this.age = age || -1;
}

Person.prototype.greet= function(){
   console.log('Hi! My name is' + this.name + '. Old ' + this.age + ' I am.');
}

var person = new Person("John", 12);
person.greet();

#1


11  

Check this JavaScript Module Pattern and this Learning JavaScript Design Patterns

检查此JavaScript模块模式和此学习JavaScript设计模式

Module example:

模块示例:

var MyModule = (function($){
  var MY_CONSTANT = 123;

  var _myPrivateVariable = 'TEST MEH';
  var _$myPrivateJqueryObject = $('div.content');

  var _myPrivateMethod = function(){
    alert('I am private!');
  };

  var myPublicMethod = function(){
    console.log('Public much?');
  }

  return {
      myPublicMethod : myPublicMethod 
  };

})(jQuery);

MyModule.myPublicMethod();

Class example:

类示例:

function Person(name, age){
   this.name = name || '';
   this.age = age || -1;
}

Person.prototype.greet= function(){
   console.log('Hi! My name is' + this.name + '. Old ' + this.age + ' I am.');
}

var person = new Person("John", 12);
person.greet();