在Javascript中实现依赖于jquery的模块模式
Implementing module pattern in Javascript with dependency on jquery
当模块代码依赖第三方库(例如jQuery)时,实现模块模式的最佳方式是什么?
var someModule = (function(){
//private attributes
var privateVar = 5;
//private methods
var privateMethod = function(){
return 'Private Test';
};
return {
//public attributes
publicVar: 10,
//public methods
publicMethod: function(){
return ' Followed By Public Test ';
},
//let's access the private members
getData: function(){
//make ajax call and do some processing and generate output
return privateMethod() + this.publicMethod() + privateVar;
}
}
})(); //the parens here cause the anonymous function to execute and return
someModule.getData();
我的问题是:我计划把所有的代码在一个像javascript文件的时尚库。
正如您在getData()
方法中看到的,我计划进行ajax调用。我想使用jQuery库。现在我如何编写javascript模块,而依赖于jQuery ?
我应该让我的整个库一个jQuery插件代替吗?
可以在这里或这里找到一个很好的教程/示例。我知道这不是模块模式,但它提供了与揭示模块模式相同的好处,并且允许您在需要时跨文件扩展名称空间。下面是该示例的代码:
//Self-Executing Anonymous Func: Part 2 (Public & Private)
(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 );
};
//Private Method
function addItem( item ) {
if ( item !== undefined ) {
console.log( "Adding " + $.trim(item) );
}
}
}( window.skillet = window.skillet || {}, jQuery ));
//Public Properties
console.log( skillet.ingredient ); //Bacon Strips
//Public Methods
skillet.fry(); //Adding Butter & Frying Bacon Strips
//Adding a Public Property
skillet.quantity = "12";
console.log( skillet.quantity ); //12
//Adding New Functionality to the Skillet
(function( skillet, $, undefined ) {
//Private Property
var amountOfGrease = "1 Cup";
//Public Method
skillet.toString = function() {
console.log( skillet.quantity + " " +
skillet.ingredient + " & " +
amountOfGrease + " of Grease" );
console.log( isHot ? "Hot" : "Cold" );
};
}( window.skillet = window.skillet || {}, jQuery ));
try {
//12 Bacon Strips & 1 Cup of Grease
skillet.toString(); //Throws Exception
} catch( e ) {
console.log( e.message ); //isHot is not defined
}
您可以尝试这样做:
var someModule = (function($){
//private attributes
var privateVar = 5;
//private methods
var privateMethod = function(){
return 'Private Test';
};
return {
//public attributes
publicVar: 10,
//public methods
publicMethod: function(){
return ' Followed By Public Test ';
},
//let's access the private members
getData: function(){
//make ajax call and do some processing and generate output
$.ajax( /* ... */ );
return privateMethod() + this.publicMethod() + privateVar;
}
}
})(jQuery); //the parens here cause the anonymous function to execute and return
someModule.getData();
请确保在执行此代码之前包含jQuery.js
相关文章:
- Javascript,访问一个主要对象模块模式中的每个对象
- 模块模式和这个
- 显示模块模式在Knockout中设置模型的新实例
- Javascript中的模块模式和揭示模块模式是否仅在创建API时有用;s
- JavaScript模块模式-如何在使用对象/函数之前激发构造函数/init函数
- TypeScript代码类似于揭示模块模式结构
- 为什么在javascript中的模块模式中实现Lazy函数时范围会发生变化
- JavaScript:模块模式差异
- 显示模块模式中的私有成员
- 如何将window.setTimeout与javascript和模块模式一起使用
- j查询模块模式未命名 |如何访问“$”
- 挖空和显示模块模式的数据绑定问题
- 传递参数时如何避免模块模式中的 getter/setter 函数
- JavaScript 模块模式给出了意想不到的结果
- 从模块模式开始
- 试图通过模块模式在DOM元素上实现change()事件
- 揭示模块模式、KnockoutJS和CoffeeScript
- 使用模块模式时访问父作用域中的变量
- 模块模式:函数未定义
- 如何用模块模式扩展javascript中的事件委派