Fire a local function from inside an imported module in JS - javascript

I am trying to run a local function, but I want to fire it from inside an imported module. This would be a sketch of how it should work... Any suggestions?
index.js
import { runBar } from "myModule.js";
runBar();
function foo() {
console.log('foo should run whenever bar is executed');
}
myModule.js
export function runBar(){
bar();
}
function bar() {
console.log("bar is running...");
//I want to call foo from here...
}

What about passing foo as a parameter?
index.js
import { runBar } from "myModule.js";
runBar(foo);
function foo() {
console.log('foo should run whenever bar is executed');
}
myModule.js
export function runBar(foo){
bar(foo);
}
function bar(foo) {
console.log("bar is running...");
//I want to call foo from here...
foo();
}

You can't call foo from bar, because foo isn't in scope.
To run foo, your options are:
Pass it to runBar and have runBar pass it to bar; or
Export foo and have myModule.js import it (cyclic dependencies are okay); or
Make foo a global (globalThis.foo = function foo() {/*...*/}, or possibly with window instead of globalThis), but I don't recommend doing that.

Related

call a function within exports.run()

I have a file called something.js with this code:
exports.run = (args) => {
function foo() {
console.log("foo");
}
}
Now I want to call foo from another file.
My code looks like this with errors attached as comments:
const something = require("./something.js");
module.exports = {
callFoo: function() {
something.foo(); //TypeError: something.foo is not a function
something.run.foo(); //TypeError: something.run.foo is not a function
}
Is it possible to call such a function which is apparently not a function?
You assign an arrow function to exports.run and in that function, you define the foo function, but this function is not reachable from outside of the arrow function. Yur code is nearly equivalent to:
exports.run = function(args) {
function foo() {
console.log("foo");
}
}
You most likely want to write:
exports.run = {
foo() {
console.log("foo");
}
}
Or
exports.run = {
foo : function() {
console.log("foo");
}
}

Sinon not working for exported function

I have a really simple JS lib (called trysinon.js) that looks like this:
export function foo() {
bar();
}
export function bar() {
return 2;
}
And I have the following test
import expect from 'expect';
import sinon from 'sinon';
import * as trysinon from 'trysinon';
describe('trying sinon', function() {
beforeEach(function() {
sinon.stub(trysinon, 'bar');
});
afterEach(function() {
trysinon.bar.restore();
});
it('calls bar', function() {
trysinon.foo();
expect(trysinon.bar.called).toBe(true);
});
});
And the test is failing. How can I ensure the test passes?
Because in foo(), you called bar() which is inner function of trysinon.js. This bar() is different with the bar() you stubbed which is exported. The best way is to change trysinon to class, or called exported bar() in foo() as following.
function bar() { return 2; }
module.exports.bar = bar;
function foo() {
module.exports.bar();
}
module.exports.foo = foo;
then you can stub bar() with sinon.stub(trysinon, 'bar').returns(2)
Hope this can help you.
I use arrow function instead, and it works.
export const foo = () => {
bar();
}
export const bar = () => {
return 2;
}

Possible strange behaviour with ES6 deconstruction syntax [duplicate]

This question already has answers here:
Babel 6 changes how it exports default
(4 answers)
Closed 6 years ago.
I've been using the deconstruction syntax { ...variable } for quite awhile now, but I've never really had a problem with it until today, while most of my use cases still work as expected this one is a bit confusing to myself.
I have a JS file that generates an object and exports it, ex:
var exports = {}
...
export default exports;
There are not any nested objects and by the end of the file it's a simple KVP.
When trying to import from this file any objects I attempt to get through deconstruction are undefined. For example:
import { Foo, Bar } from './my-object';
Foo.bar(); // Cannot read property bar of undefined
However, if I break it apart farther like so, everything is fine:
import MyObject from './my-object';
const { Foo, Bar } = MyObject;
Foo.bar(); // Works!
I've tried changing exports to a different variable name as I thought maybe, just maybe it was a confliction with module.exports, but that wasn't the problem.
In the past whenever I exported an object it was simple:
export default { ... }
I'm really confused on what the issue could be this time around as the result of console.log(exports) is the same thing:
{ Foo: foo, Bar: bar }
Where bar() is a function variable of foo
I should also add that trying to hack this to have the proper results doesn't work either, for example:
export default {
Foo: { bar: () => {} },
Bar: { foo: () => {} }
};
Still throws the same Cannot read property __ of undefined
If you are using Babel, it's because the export default statement is roughly translated to:
var foo = {};
exports["default"] = foo;
And import MyObject from './my-object' is translated to: var MyObject = require('./my-object').default;. Which is why your second example works.
However, when you're doing import { Foo, Bar } from './my-object', it is translated to var { Foo, Bar } = require('./my-object');
See this question for extra details.
In your case, I recommend just using the normal export statement. For example:
export class Foo {
myMethod() {}
};
export const Bar = { a: '1' };
Then you can do import { Foo, Bar } from './my-object';.
I think You just miss the basic concept.
In this case You are exporting a single object not multiple classes that is why your import is not working as you are thinking.
The syntax works when we have a file say sample.js with
export var A = (function () {
function A() {
}
return A;
}());
export var B = (function () {
function B() {
}
return B;
}());
export var C = (function () {
function C() {
}
return C;
}());
And if you are importing in other file with the following manner :
import { A, B, C } from './sample';
I have not tried this but I think It is working like this.
if you want to import { Foo, Bar} from './my-object', you need to explicitly export the Foo and Bar. e.g.
export const Foo = { bar() {} };
export const Bar = { foo() {} };

How to call base class method instead of extended class method

In the following example, how can I directly call Base.foo() from Base.bar(), I understand why it's calling Test.foo() first, but is there any way to prevent this.
class Base {
constructor() {
console.log('Base constructor')
}
foo() {
console.log('Base foo')
}
bar() {
console.log('Base bar')
this.foo();
}
}
class Test extends Base {
foo() {
console.log('Test foo');
super.foo();
}
bar() {
console.log('Test bar');
super.bar();
}
}
const test = new Test();
test.bar();
Output would be:
Base constructor
Test bar
Base bar
Test foo
Base foo
My expected output would be:
Base constructor
Test bar
Base bar
Base foo
You can use standard JavaScript features while using ES2015 classes. In this case, simply use Function.prototype.call():
'use strict'
class Base {
constructor() {
console.log('Base constructor')
}
foo() {
console.log('Base foo')
}
bar() {
console.log('Base bar')
Base.prototype.foo.call(this);
}
}
class Test extends Base {
foo() {
console.log('Test foo');
super.foo();
}
bar() {
console.log('Test bar');
super.bar();
}
}
const test = new Test();
test.bar();

Getting error when import instanceMethods function in Sequelize

I have created inside a file a function and export it using
export function foo(params...) {
// do something
}
inside the initialization of the model I import the function in this way:
import { foo } from "../path/..."
instanceMethods: {
foo: foo
}
the problem is the model is not initialized correctly. Do you know why?
the problem is the model is not initialized correctly
The following code works fine.
export function foo(params...) {
// do something
}
import { foo } from "./foo";
let instanceMethods = {
foo: foo
};
Error is elsewhere. Perhaps you meant to use = and used :? Note that if you don't use the variable it is erased form the output (ref).
#)-'--

Categories

Resources