I am trying to call an exported function inside the nodejs module.
exports.sayHello = function (msg) {
console.log(msg)
}
function run(msg) {
this.sayHello(msg);
}
run("hello");
when I run this script I got TypeError: this.sayHello is not a function
Simply declare it separately from exporting it (and don't use this when calling it, you haven't attached it to an object):
function sayHello(msg) {
console.log(msg)
}
exports.sayHello = sayHello;
function run(msg) {
sayHello(msg);
}
run("hello");
That said, you could call it via exports:
exports.sayHello = function (msg) {
console.log(msg)
}
function run(msg) {
exports.sayHello(msg); // <===
}
run("hello");
...but that seems a bit roundabout to me, though I'm told it can help with testing, such as in this example.
Related
We tried to port the following code to ES6:
function apitest(data) {
data.cb(true);
}
function test(cb) {
apitest({cb: function(data) {
commit(cb,data);
}});
function commit(cb,data) {
cb(data);
}
}
test(data => {
document.write(data);
});
It might look a little confusing, but it does what we expect (return true) and does not throw errors.
However, Babel transpiles it to:
"use strict";
function apitest(data) {
data.cb(true);
}
function test(_cb) {
apitest({ cb: function cb(data) {
commit(_cb, data);
} });
function commit(_cb, data) {
cb(data);
}
}
test(function (data) {
document.write(data);
});
//# sourceMappingURL=test4.js.map
This code fails since the cb() called inside commit() does not have an underscore.
Regardless of whether you should write this kind of code: Is our syntax faulty or is this a bug in Babel?
My understanding is that the definition of cb inside the object should mask the passed parameter. Babel assigns different names to the variable used in the object and in the enclosing function while giving a name to the anonymous function (why would it do that anyway?). After that, it should rename the function call inside commit().
This was a bug in Babel 5 and it's fixed in Babel 6.
I'm working on a big project and I simplified what it matters here. This is the code:
a = new Thing(/*sayHi + sayHey*/);
function sayHi() {
alert("hi");
}
function sayHey() {
alert("hey");
}
function Thing (callback) {
callback();
}
I'd like to, with just the callback parameter, call both the sayHi() and the sayHey() function, at the order I put them. Is it possible? How would I do it? Thank you.
Pass an anonymous function that calls both of them sequentially:
a = new Thing(function() {
sayHi();
sayHey();
});
function sayHi() {
alert("hi");
}
function sayHey() {
alert("hey");
}
function Thing (callback) {
callback();
}
Alternatively to #Barnar's answer, create and pass a regular named function. If the callback logic gets heavier, you might want that anyway.
function hiHeyCallback() {
sayHi();
sayHey();
}
a = new Thing(hiHeyCallback);
I have a hub server method called with signalr many times.
I know i can do:
hub.server.method().done(function(data){
//my_code
}
but is there any way I can set a 'default' done() function so I don't have to repeat it every time I call that method?
Create a function in your *.js file like this
function doSomething(){
hub.server.method().done(function(data){
//my_code
}
};
And then call doSomething instead of
hub.server.method().done(function(data){
//my_code
}
Edited:
If you want to add a function to the server object you can do:
hub.server.myDecoratedBehavior= function() {
if (connected) {
this.originalBehavior().done(function () {
console.log("Chat window was cleared");
});
}
};
var onDone = function(data) {
// code
};
hub.server.method1().done(onDone);
hub.server.method2().done(onDone);
I have an AngularJS factory that has multiple functions.
I want to call one of the functions inside the other function as shown below:
.factory("AppStart", function($cordovaSQLite) {
return {
init: function() {
var res = "hello";
console.log("in load start up page");
},
create_table: function() {
AppStart.init();
}
}
});
But I get the following error:
AppStart is not defined.
So how do I call the init() function in the create_table() function? I have tried just calling init(), but it doesn't work either.
To accomplish this, I recommend defining your functions with names, and then creating a service object with properties that refer to them, as I did below:
.factory("AppStart", function($cordovaSQLite) {
function init() {
var res = "hello";
console.log("in load start up page");
}
function create_table() {
init();
}
return {
init: init,
create_table: create_table
};
});
Just wondering if there is anyway to fire some code when a function is called, without adding the code to the function, for example:
function doSomething(){
//Do something
}
//Code to call when doSomething is called
You can wrap the function :
(function(){
var oldFunction = doSomething;
doSomething = function(){
// do something else
oldFunction.apply(this, arguments);
}
})();
I use an IIFE here just to avoid polluting the global namespace, it's accessory.
Well, yes, it's not actually hard to do. The crucial thing is that a function's name is just an identifier like any other. You can redefine it if you want to.
var oldFn = doSomething;
doSomething = function() {
// code to run before the old function
return oldFn.apply(this, arguments);
// code to run after the old function
};
NB that it's better to do oldFn.apply(this, arguments) rather than just oldFn. In many cases it won't matter, but it's possible that the context (i.e. the value of this inside the function) and the arguments are important. Using apply means they are passed on as if oldFn had been called directly.
What about something like:
function doSomething(){
doSomething.called = true;
}
//call?
doSomething();
if(doSomething.called) {
//Code to call when doSomething is called
}
I know you said you don't want to modify the original function, but consider adding a callback. Then you can execute code based on different results in your function (such as onSucess and onError):
function doSomething(onSuccess, onError){
try {
throw "this is an error";
if(onSuccess) {
onSuccess();
}
} catch(err) {
if(onError) {
onError(err);
}
}
}
Then, when you call doSomething, you can specify what you want done with inline functions:
doSomething(function() {
console.log("doSomething() success");
}, function(err) {
console.log("doSomething() error: " + err);
});