foo.js
const a = 1
const b = 2
const c = 3
let d = 4
export default { a, b }
export { c }
export d
What's the difference between exported a, b, c, d?
And how to import them correctly?
Does the Babel compilation will effect it?
You can only export declarations (except for the default export) that means that:
export d;
is invalid, it has to be:
export const d = 1;
And how to import them correctly?
import main, { c, d } from "sth";
const {a, b} = main;
You can import c and d in the same way, only the object properties a and b can't be accesed directly as you can't destructure objects inside the import statement, therefore you have to destructure them in a new line which is just ugly. To quote Bergi: "Don't do that", instead go with c or d.
Related
I have a code that has complex function calls stack. I would like to refactor it which avoids calling function too deep. Currently, the function call stack looks like this:
function A () {
a = 1
return B(a)
}
function B (b) {
return C(b+1)
}
function C (c) {
return D(c+1)
}
function D (d) {
return (d+1)
}
This is what I would like to re-write look like:
function A () {
a = 1
b = B(a) // b -> 2
c = C(b) // c -> 3
d = D(c) // d -> 4
}
I'm not sure which one is a better solution. I feel like that calling B, C, D at the same level could make the code flow easier to be traced and easier to understand when the implementation in B, C, D getting more complicated. Is there any opinion of both benefits or drawbacks?
Thanks!
There is a specific thing i want to do from time to time, that i cannot figure out:
suppose module1.js exports 3 values:
//module1.js
export const a = 'a';
export const b = 'b';
export const c = 'c';
And then, in module2.js, I want to import two of these, into an object (as a kind of namespace thing):
//module2.js
import { a, c } as constants from './module1'; //<-WRONG!
So what I end up doing is something like this:
//module2.js
import { a, c } from './module1';
const constants = { a, c };
This works, but now a and c exist both in constants and also directly in the module scope.
Is there a way to avoid this?
As per MDN documentation, you can either set an alias on entire module contents such as * as constants or a single content such as b as constants. But you can't set an alias on specific contents. So one of the solutions would be using *.
import * as constants from './module1';
Another possible solution would be passing { a, c } as default.
//module1.js
export const a = ...
export const b = ...
export const c = ...
export const d = ...
export default { a, b, c };
/module2.js
import contants from './someModule';
doStuff(constatns);
Lastly, If you don't want to pass these constants as default, you can create an object and pass that object.
//module1.js
export const a = ...
export const b = ...
export const c = ...
export const b = ...
export const myCustomConstants = { a, c };
//module2.js
import { myCustomConstants } from './someModule';
doStuff(myCustomConstants);
Do you mean something like
import * as constants from './module1';
You could also remove some if you need to pass them down, something like lodash pick
const cleanConstants = _.pick(['a', 'c'], constants);
Here is chisel3 test that uses ScalaCheck to perform property checking on a simple combinational circuit.
package stackoverflow
import org.scalatest.{ Matchers, FlatSpec, GivenWhenThen}
import org.scalacheck.{ Properties, Gen, Arbitrary}
import org.scalacheck.Prop.{ forAll, AnyOperators, collect}
import chisel3._
import firrtl_interpreter.InterpretiveTester
object G {
val width = 8
}
class Add extends Module {
val io = IO(new Bundle {
val a = Input(UInt(G.width.W))
val b = Input(UInt(G.width.W))
val o = Output(UInt(G.width.W))
})
io.o := io.a + io.b
}
class AddTester {
val s = chisel3.Driver.emit( () => new Add)
val tester = new InterpretiveTester( s)
def run( a : Int, b : Int) = {
val result = (a + b) & ((1 << G.width)-1)
tester.poke( s"io_a", a)
tester.poke( s"io_b", b)
tester.peek( s"io_o") ?= result
}
}
object AddTest extends Properties("Add") {
val t = new AddTester
val gen = Gen.choose(0,(1 << G.width)-1)
property("Add") = forAll( gen, gen) {
case (a:Int,b:Int) => t.run( a, b)
}
}
This uses the firrtl interpreter directly. Does anyone know how to do something similar using the PeekPokeTester so I can use the verilator and vcs backends as well?
Is this close to what you have in mind? It's a lot more scalatesty in form. I haven't been able to get the gen stuff working here (there is some kind of weird interaction with chisel), and I'm more familiar with FreeSpec so I started with it. I also threw a printf and a println in so you could believe it was working. This works with the interpreter backend as well.
import org.scalatest.FreeSpec
import org.scalacheck.Prop.AnyOperators
import chisel3._
import chisel3.iotesters.PeekPokeTester
class Add2 extends Module {
val io = IO(new Bundle {
val a = Input(UInt(G.width.W))
val b = Input(UInt(G.width.W))
val o = Output(UInt(G.width.W))
})
io.o := io.a + io.b
printf("%d = %d + %d\n", io.o, io.a, io.b)
}
class ScalaTestyTester extends FreeSpec {
"scalatest verilator test" in {
iotesters.Driver.execute(Array("--backend-name", "verilator"), () => new Add2) { c =>
new PeekPokeTester(c) {
for(_ <- 0 until 10) {
val a = BigInt(G.width, scala.util.Random)
val b = BigInt(G.width, scala.util.Random)
println(s"testing a = $a b = $b")
val result = (a + b) & ((1 << G.width) - 1)
poke(c.io.a, a)
poke(c.io.b, b)
step(1)
peek(c.io.o) ?= result
}
}
}
}
}
I want my module to export multiple functions and numerical constants.
When I import all exported properties of my module I see that functions are not actually imported.
//module1.js
const func1 = (a) => {console.log(1)};
const func2 = (b) => {console.log(2)};
const variable1 = 1;
const variable2 = 2;
export const exp1 = func1;
export const exp2 = func2;
export const exp3 = variable1;
export const exp4 = variable2;
.
//anotherFile.js
import * as module1 from './module1';
console.log(JSON.stringify(module1, null, 2)); // {"module1": {"exp3":1, "exp4":2}}
What is the correct way of importing functions?
All works!
my problem was in console.log(JSON.stringify(module1, null, 2));
JSON.stringify was cutting off my functions
As in the title: does TypeScript support namespaces? If so, how do I use them?
Typescript allows to define modules closely related to what will be in ECMAScript 6. The following example is taken from the spec:
module outer {
var local = 1;
export var a = local;
export module inner {
export var x = 10;
}
}
As you can see, modules have names and can be nested. If you use dots in module names, typescript will compile this to nested modules as follows:
module A.B.C {
export var x = 1;
}
This is equal to
module A {
module B {
module C {
export var x = 1;
}
}
}
What's also important is that if you reuse the exact same module name in one typescript program, the code will belong to the same module. Hence, you can use nested modules to implement hierarchichal namespaces.
As of version 1.5, Typescript supports namespace keyword. Namespaces are equivalent to internal modules.
From What's new in Typescript:
Before:
module Math {
export function add(x, y) { ... }
}
After:
namespace Math {
export function add(x, y) { ... }
}
For defining an internal module, now you can use both module and namespace.
Here is a TypeScript namespace example:
///<reference path='AnotherNamespace/ClassOne.ts'/>
///<reference path='AnotherNamespace/ClassTwo.ts'/>
module MyNamespace
{
import ClassOne = AnotherNamespace.ClassOne;
import ClassTwo = AnotherNamespace.ClassTwo;
export class Main
{
private _classOne:ClassOne;
private _classTwo:ClassTwo;
constructor()
{
this._classOne = new ClassOne();
this._classTwo = new ClassTwo();
}
}
}
There is no 'namespace' keyword, but internal modules (using the 'module' keyword) and external modules (using the 'export' keyword) offer a similar way to partition your code into logical hierarchies.
False...
module A.B.C {
export var x = 1;
}
is equal to
module A {
export module B {
export module C {
export var x = 1;
}
}
}
because you can write outside the module A :
var y = A.B.C.x;
But :
module A {
module B {
module C {
export var x = 1;
}
var y = C.x; // OK
}
//var y = B.C.x; // Invalid
}
//var y = A.B.C.x; // Invalid