Revealing module implicity calling an internal function - is this a "smell" [closed] - javascript

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I'm trying to improve my javascript and have been using the revealing module pattern to good effect but I noticed that code in the module not in a function or the return is executed on creation have utilised this, such as:
var MyModule = function ($) {
setUp();
function setUp() {
// execute code to be run when module created
}
function doStuff() {
}
return {
doStuff: doStuff
}
}
I can see this is creating a form of constructor for the function which relies on it being called by var myModule = new MyModule(jQuery).
Any feedback on the rights or wrongs of this appreciated.

Related

How to migrate javascript file into typescript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I need some help/suggestion on how can I able to migrate javascript file into typescript.
I am very early beginner on typescript.
I have gone through many tuorials as well https://www.typescriptlang.org/docs/handbook/classes.html but not able to do so .
Any help/suggestion would be very helpful.
//eaxmple code
(function() {
"use strict";
var CONSTANTS = {
PROTOCOL_SECURE : "https://",
PROTOCOL : "http://"
};
var constantNew = window.constantNew = function() {};
constantNew.prototype.init = function(params) {
this.lobby = params.lobby;
this.bSecure = !!params.bSecure;
};
constantNew.prototype.getPMAInfo = function() {
var url = (this.bSecure? CONSTANTS.PROTOCOL_SECURE:DEMETER_CONSTANTS.PROTOCOL)
+this.lobby
+CONSTANTS.URL_GETPMAINFO_PATH;
return Promise.resolve({baseUrl:url})
.then(callDemeter) // just a call to demeter to get PMA Info
.then(decoratePmaData) // a small decorator
.catch(checkGeneralFailure("getPMAInfo"));// final error handler
};
constantNew.prototype.hangoutAuthenticate = function(args) {
var baseUrl = (this.bSecure? DEMETER_CONSTANTS.PROTOCOL_SECURE:DEMETER_CONSTANTS.PROTOCOL)
+this.lobby
+DEMETER_CONSTANTS.URL_HANGOUT_AUTHENTICATE;
return Promise.resolve()
.then(requestChallengeFn(baseUrl)) // request challenge (higher order function to get the workflow function)
.then(authenticatePartialApplication(baseUrl, args.account, args.password, args.bValidateByConfCode)) // try first first auth round or fail
.then(decorateHangoutAuthResult) // decorate result to return
.catch(checkGeneralFailure("hangoutAuthenticate")); // final error handler
function decorateHangoutAuthResult(authResult) {
return {
strWebroom: authResult.webroom[0].webroomcode,
strWebroomResource: authResult.webroom[0].resource,
strDemeterAuthToken: authResult.AUTHTOKEN,
strAudioConfCode: authResult.webroom[0].audioconferencecode
};
}
};
}());
There's no direct way of doing it unfortunately (i.e., via a script or some utility). Typescript provides the following guide to get you running there: https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html
That being the case, it might be better to start over. There's several options such as the Typescript Node Starter project in Github: https://github.com/microsoft/TypeScript-Node-Starter

Jest testing a function with no return [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am in the process of unit testing a function using Jest that merely passes on its parameters to another function by calling it and has no return.
I have googled and tried a bunch of suggested mock implementations, but can't seem to get it to work.
Imported File:
export const firstFunc = (dataObj) => {
secondFunc('anything', dataObj)
}
export const secondFunc = (str, dataObj) => {
// Does something with the dataObj but also doesn't return anything.
}
Essentially what I want to test for is that firstFunc calls secondFunc, but am not able to get it to work.
Thank you in advance. Also, open to suggestions for any other tests that I could run in this situation.
As far as I understood your problem, you could have two solutions.
Firstly, add return value after calling function, and export it to tests.
Secondly, you could use sinon js with jest.
Spying on an imported function that calls another function in Jest

Promise chain/general code convention/style [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
There is some promise convention best practice which I can use as reference.
I mean something like when you write then you can do it like this
step1().then(step2()).then(retur ...step3())
.catch(function(err) {
console.log(err)
;});
but I think the more readable way is like this
step1()
.then(function() {
return step2();
}).then(function() {
return step3()
}).catch(function(err) {
log(err);
});
There is some official recommendation how to its better to write it in term of readability etc...
Creating intermediate closures is of no benefit if you already have methods ready to hook up.
Your two examples are also doing entirely different things. Your first example should look like this.
step1()
.then(step2)
.then(step3)
.catch(function(err) {
console.log(err)
;});
I don't think there's anything wrong with that. This is also gonna make your stack traces more readable as well.
This is, because, in this example, you have a bunch of intermediate anonymous functions.
As such the stack trace is gonna be containing references to those anonymous functions, while they serve no value.
step1()
.then(function(val) {
return step2(val);
}).then(function(val) {
return step3(val)
}).catch(function(err) {
log(err);
});
In order to prevent the anonymous clutter, you would want to name all your anonymous methods like so.
step1()
.then(function _step2(val) {
return step2(val);
}).then(function _step3(val) {
return step3(val)
}).catch(function(err) {
log(err);
});
I'm prefixing them with an underscore as they can't have the same name as the actual method we are calling off course.
This is a bunch of duplication and it doesn't make much sense because the _step2 and step2 functions are entirely identical.
Also note that in your preferred example you are not passing the parameters correctly, I have added that logic above as well, which as you can see, further increases the noise.

Testing Error on node.js function with Mocha [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
With Node.js installed and testing with mocha,
I have two files, numbers.js and test.js in the same directory
Following the top answer: What is the purpose of Node.js module.exports and how do you use it?
numbers.js
var myFunc = function myFunc() {
return 10;
};
exports.myFunc = myFunc;
test.js
var assert = require('assert');
var numbers = require('./numbers');
describe('numbers', function() {
it('first function returns 10', function() {
var result = numbers.myFunc;
assert.equal(result, 10);
});
});
But when running $ mocha it returns the error:
AssertionError: [Function: myFunc] == 10
What am I doing wrong?
You need to call your myFunc function. Add the parens
describe('numbers', function() {
it('first function returns 10', function() {
var result = numbers.myFunc(); <<<<<<
assert.equal(result, 10);
});
});
First of all, Inspect your numbers module is properly required or not.
var numbers = require('../numbers'); // Check path of file numbers.js
If yes, write :
var result = numbers.myFunc(); // your module exports 'myFunc' property not 'myfunc'
assert.equal(result, 10); // your function return '10' instead of '1'

Is that possible to call module factory from other javascript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have two javascript files separately and I want to retrieve one from another. So, I created Module factory as below;
var module = angular.module('test',[]);
module.factory('$result', function() {
var data = {something:"somewhere"};
return data;
});
and I want to called from other file. For now, I settled as below;
var module = angular.module('myApp', ['test']);
module.controller('NearestController', function($test) {
console.log(test);
});
Which part of me doing wrong ?? please help me and I'm newbie in angular
It should be like that ..
var module = angular.module('test',[]);
module.factory('Result', function() {
var data = {something:"somewhere"};
return data;
});
You can call like that.
var module = angular.module('myApp', ['test']);
module.controller('NearestController', function(Result) {
console.log(Result);
});
Hope it might be work.
As the comments suggested, you inject your dependencies as parameters in your controller function:
var module = angular.module('myApp', ['test']);
module.controller('NearestController', function($result) {
console.log(test);
});

Categories

Resources