module-pattern

Objects literal and 'this' in submodule pattern

放肆的年华 提交于 2020-01-16 19:39:11
问题 Im working in a sub-module pattern code. Want to create sub-modules with objects literals, the problem is this for the objects inside the sub-module is MODULE and not my object literal. Any idea? var MODULE.sub = (function () { var myObject = { key: value, method: function () { this.key // this = MODULE and not MyObject... :( } }; return myObject.method; }(MODULE)); 回答1: This works for me: var MODULE = MODULE || {}; MODULE.sub = (function () { return { myObject : { key : 10, method : function

Objects literal and 'this' in submodule pattern

孤者浪人 提交于 2020-01-16 19:38:12
问题 Im working in a sub-module pattern code. Want to create sub-modules with objects literals, the problem is this for the objects inside the sub-module is MODULE and not my object literal. Any idea? var MODULE.sub = (function () { var myObject = { key: value, method: function () { this.key // this = MODULE and not MyObject... :( } }; return myObject.method; }(MODULE)); 回答1: This works for me: var MODULE = MODULE || {}; MODULE.sub = (function () { return { myObject : { key : 10, method : function

Javascript mixins when using the module pattern

删除回忆录丶 提交于 2019-12-28 03:06:05
问题 I've been using the module pattern for a while, but recently have started wanting to mix in functions and properties into them to increase code re-use. I've read some good resources on the subject, but still am a bit uncertain as to the best approach. Here is a module: var myModule = function () { var privateConfigVar = "Private!"; //"constructor" function module() {} module.publicMethod = function () { console.log('public'); } function privateMethod1() { console.log('private'); } return

Implementing module pattern in Javascript with dependency on jquery

无人久伴 提交于 2019-12-24 04:17:15
问题 What is the best way to implement module pattern, while the module code depends on third party libraries like jQuery for example? 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

(Revealing) Module Pattern, public variables and return-statement

核能气质少年 提交于 2019-12-24 00:42:39
问题 I'm trying to understand how public` properties in the (Revealing) Module Pattern work. An advantage pointed out by Carl Danley "The Revealing Module Pattern" is: Explicitly defined public methods and variables which lead to increased readability Let's take a look at this code (fiddle): var a = function() { var _private = null; var _public = null; function init() { _private = 'private'; _public = 'public'; } function getPrivate() { return _private; } return { _public : _public, init : init,

How can I safely access other sibling functions and variables in a Javascript Module Pattern without accessing something in the containing scope?

别来无恙 提交于 2019-12-23 15:34:06
问题 I have a Javascript Object structured after the Module Pattern. I have several private function in it which are called from other sibling "private" functions. How can I access another variable/function without the potential to accidentally access a global/external variable/object/function? function doSomething() { alert("Something I don't want to do"); } var My.Namespaced.SingletonClass = (function() { var init = function() { doSomething(); } var doSomething = function() { alert("Something I

JavaScript Design Patterns Help Needed: Loose Augmentation of Modules

末鹿安然 提交于 2019-12-23 07:57:25
问题 Edit for clarity - @Qantas94Heavy - I understand what it is "saying" or supposed to do, what I don't understand is why & more importantly how it works: I was reading an advanced tutorial on the JS Module Pattern, and it gave this example: var MODULE = (function (my) { // add capabilities... return my; }(MODULE || {})); The thing that is bugging me (and I need your help with) is the last statement: (MODULE || {})); i'm having trouble understanding the syntax rules behind this that make it

how to use the javascript module pattern in a real example?

老子叫甜甜 提交于 2019-12-20 08:46:13
问题 I am trying to understand the JavaScript Module Pattern. I've seen examples of what it should look like, but I don't understand how to use it. For example, a few things are happening here: $('input#share').on("click", function() { $('.loading').html('<img class="remove_loading" src="/graphics/loading.gif" />'); var message = $(".wallmessage").val(); if (message == ""){ $("#messageempty").jmNotify(); $('.remove_loading').remove(); } else { addMessage(message); } return false; }); function

Module pattern- How to split the code for one module into different js files?

我的未来我决定 提交于 2019-12-18 10:17:08
问题 For the module pattern, I'm doing something like: (function(namespace) { // tons of code // blabla })(window.myGlobalNamespace); How do I split the code? I can think of a few ways, like use a hierachy of namespaces, or expand the object outside by window.myGlobalNamespace.additionalFunc = function () {//blabla} . What are the other ways? What are the pros and cons? Which one is considered better practice? Both of the two answers suggest RequireJS. Can you please explain how RequireJS can

Inheritance and module pattern

落花浮王杯 提交于 2019-12-18 01:19:27
问题 I'm trying to implement the inheritance with the module pattern in this way: Parent = function () { //constructor (function construct () { console.log("Parent"); })(); // public functions return this.prototype = { test: function () { console.log("test parent"); }, test2: function () { console.log("test2 parent"); } }; }; Child = function () { // constructor (function () { console.log("Child"); Parent.call(this, arguments); this.prototype = Object.create(Parent.prototype); })(); // public