revealing-module-pattern

JS: revealing module pattern - accessing internal objects vs arrays?

自闭症网瘾萝莉.ら 提交于 2020-01-25 12:24:05
问题 Using the revealing module pattern, how can I provide direct access to non-static private variables? Here's what I have: var M = function () { var obj = {}; var arr = []; var change = function () { obj = {"key":"if I see this, O is a reference to obj"}; arr.push("If I see this, A is a reference to arr") }; return { change: change, O: obj, A: arr }; }(); M.change(); console.log(M.A); // prints ["If I see this, A is a reference to arr"] console.log(M.O); // prints Object {}, wanted "if I see

Slight variation of the Revealing Module Pattern

与世无争的帅哥 提交于 2020-01-06 19:36:08
问题 Is it good practice to use, instead of this Revealing Module Pattern ... var MyModule = ( function() { function Method1() { alert( 'method1' ); } function Method2() { Method1(); alert( 'method2' ); } function Method3() { Method1(); alert( 'method3' ); } function Method4() { Method1(); alert( 'method4' ); } return { Method1 : Method1, // these Method2 : Method2, // lines Method3 : Method3, // are Method4 : Method4 }; // redundant... } )(); MyModule.Method1(); MyModule.Method2(); ... this

TypeScript code similar to Revealing Module Pattern structure

こ雲淡風輕ζ 提交于 2020-01-02 06:19:27
问题 I want to convert some JavaScript code I've written into TypeScript. I'm rather new to TypeScript syntax and way of thinking, as a JavaScript developer. What is giving me a headache is the hard time I've had to convert some piece of code that uses the Revealing Module Pattern into TypeScript. One example is the below: //JS Code var obj; //code... (function(){ function myFunction(){ //do work } function MyOtherConstructor(){ return { publicMethod: myFunction } } obj = new MyOtherConstructor();

(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 view the outline in eclipse when using the revealing module pattern?

邮差的信 提交于 2019-12-18 11:26:43
问题 I'm currently refactoring some Javascript code we have and amongst other things I've changed it to make use of the revealing module pattern. The code is looking much tidier and it works fine but I can't see the functions anymore in the outline view. I see the top level namespace var as a var but you can't expand it to see the functions within. Lets say the code used to look like this: function myFunc1() {} function myFunc2() {} In this case you see both functions in the outline view. But if

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

Returning data resolved by XMLHttpRequest with module pattern function

寵の児 提交于 2019-12-12 05:08:39
问题 I have problem combining javascript callbacks and revealing module pattern. What I'm trying to do is to return the HTTP response text with the carsData.getCars() function method. Basically what I want to do is: return the data from xhr.onreadystatechange function to the private getData function return the data from getData function to the public getCars function ( or call the getCars function returning a value) I got it to work with the synchronous AJAX mode, but I'm aware it's not the best

Javascript: Mixing constructor pattern and Revealing Module Pattern

情到浓时终转凉″ 提交于 2019-12-11 02:08:51
问题 Is there any way I can do have a Javascript class that extends an object that was created through the revealing module pattern? I tried the following code, but is there away to achieve the same thing? sv.MergeQuestionViewModel = function () { this = sv.QuestionDetailViewModal(); this.init($("#mergeQuestionModel")); }; sv.QuestionDetailViewModal = function () { var $el, self = this, _question = ko.observable(), _status = new sv.Status(); var _init = function (el) { $el = el; $el.modal({ show:

Knockout.js: Function parameter undefined

ぃ、小莉子 提交于 2019-12-11 00:59:54
问题 I have a very simple example that is not working. jsfiddle: http://jsfiddle.net/ThomasDeutsch/8hzhp/3/ // My Model function Customer(id, name, lastname) { this.Id = ko.observable(id); this.Name = ko.observable(name); this.LastName = ko.observable(lastname); } // My ViewModel ViewModel = (function () { var getName = ko.computed(function (x) { return x.Name(); }); return { getName: getName(new Customer(1, "Thomas", "D")) }; })(); ko.applyBindings(ViewModel);​ problem: parameter (x) is undefined

Global variables shared amongst Javascript Modules

一世执手 提交于 2019-12-08 07:51:13
问题 I have a script that I'd like to seperate out to multiple modules. For example, putting all my mouse events in another module (mouseup, mousedown, etc). I use a bunch of global variables to keep track of all of my objects on the screen. The mouse module will need to access these variables and alter them. I realize I can reveal each variable needed to other modules, but they are unable to alter them. How is this best achieved using Javascript? I current use the revealing module pattern, and I