module-pattern

How can i call a public method from within a private one when using the javascript Module Pattern?

流过昼夜 提交于 2019-11-30 13:27:49
I would like to call a public method from a private one but the property "this" refers to the window object. Please note i am trying to apply the module pattern. You can find a working code example at jsfiddle.net // how can i access a public method from a private one? // (in this example publicAlert from privateMethod) // this refers to the window object. $(function() { var modulePattern = (function($) { var privateMethod = function() { appendText("called privateMethod()"); this.publicAlert(); }; var appendText = function(texToAppend) { var text = $('#output').text() + " | " + texToAppend; $(

Javascript revealing module pattern, public properties

不羁岁月 提交于 2019-11-30 12:33:43
问题 I'm trying to get my head round the revealing module pattern in javascript. I'm puzzled by two things about the following code snippet. var Child = function () { var totalPoints = 100; addPoints = function (points) { totalPoints += points; return totalPoints; }; getPoints = function () { return totalPoints; }; return { points: totalPoints, addPoints: addPoints }; }; $(function () { var george = Child(); console.log(george.points); console.log(george.addPoints(50)); console.log(george.points);

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

只谈情不闲聊 提交于 2019-11-29 20:31:26
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 solve these problems: first.js: (function(context) { var parentPrivate = 'parentPrivate'; })(window

How can i call a public method from within a private one when using the javascript Module Pattern?

百般思念 提交于 2019-11-29 18:43:33
问题 I would like to call a public method from a private one but the property "this" refers to the window object. Please note i am trying to apply the module pattern. You can find a working code example at jsfiddle.net // how can i access a public method from a private one? // (in this example publicAlert from privateMethod) // this refers to the window object. $(function() { var modulePattern = (function($) { var privateMethod = function() { appendText("called privateMethod()"); this.publicAlert(

Inheritance and module pattern

允我心安 提交于 2019-11-28 21:58:33
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 functions return this.prototype = { test: function() { console.log("test Child"); } } }; but I can't to

What is the intention of Ninject modules?

与世无争的帅哥 提交于 2019-11-27 18:40:28
I'm a complete newbie to ninject I've been pulling apart someone else's code and found several instances of nInject modules - classes that derive from Ninject.Modules.Module, and have a load method that contains most of their code. These classes are called by invoking the LoadModule method of an instance of StandardKernel and passing it an instance of the module class. Maybe I'm missing something obvious here, but what is the benefit of this over just creating a plain old class and calling its method, or perhaps a static class with a static method? Jarrett Meyer The Ninject modules are the

Javascript: Module Pattern vs Constructor/Prototype pattern?

时间秒杀一切 提交于 2019-11-27 17:04:35
I would like to know if the module pattern or Constructor/protoType pattern is more applicable to my work. Basically I am using unobtrusive javascript -- the HTML document has a reference to the .js file. My understanding of the module pattern: call an INIT method (which is basically a public method i can create and return using the module pattern) In the INIT method, assign all click events etc. This sounds like the perfect pattern for my situation, as I don't need to create Objects and inheritance hierarchies etc. My understanding of the Constructor/Prototype pattern: for creating objects

Different between Module Pattern and Singleton Pattern?

萝らか妹 提交于 2019-11-27 13:57:59
问题 I've seen that in some projects, Module Pattern uses instead of Singleton Pattern and vice versa. I want to know exactly, what's the different between Module Pattern and Singleton Pattern ? 回答1: Module pattern in javascript refers to the modularisation of code for a common mechanism. It works quite well to split one "class" across several files as you can define constructor and various groups of prototype methods independently. Each of the modules is usually wrapped inside a closure to create

Strict Violation using this keyword and revealing module pattern

ⅰ亾dé卋堺 提交于 2019-11-27 11:52:57
Having trouble getting the following to pass jslint/jshint /*jshint strict: true */ var myModule = (function() { "use strict"; var privVar = true, pubVar = false; function privFn() { return this.test; // -> Strict violation. } function pubFn() { this.test = 'public'; // -> Strict violation. privFn.call(this); // -> Strict violation. } return { pubVar: pubVar, pubFn: pubFn }; }()); myModule.pubFn(); I understand it's being caused by the use of this in a function declaration, but I read something Crockford wrote and he said the violation is meant to prevent global variable pollution - but the

Javascript mixins when using the module pattern

大兔子大兔子 提交于 2019-11-27 09:10:37
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 module; } And here is a mixin object: var myMixin = function () {}; Mixin.prototype = { mixinMethod1: