Skip to content

Instantly share code, notes, and snippets.

@kevincoleman
Created March 1, 2019 17:19
Show Gist options
  • Save kevincoleman/e460119393c8d6532691f89086fe495c to your computer and use it in GitHub Desktop.
Save kevincoleman/e460119393c8d6532691f89086fe495c to your computer and use it in GitHub Desktop.
Three import styles
// way #1
import Storage from "storage";
class MyClass {
setCache(data) {
return Storage.insert(data);
}
}
// way #2
import Storage from "storage";
const storage = new Storage();
class MyClass {
setCache(data) {
return storage.insert(data);
}
}
// way #3
import Storage from "storage";
const storage = new Storage();
class MyClass {
constructor(storage) {
this.storage = storage;
}
setCache(data) {
return storage.insert(data);
}
}
@nathan-isaac
Copy link

nathan-isaac commented Mar 15, 2019

// #4
export class Storage {}

//...
export class MyClass {
  constructor(storage) {
    this.storage = storage;
  }
  
  setCache(data) {
    return this.storage.insert(data);
  }
}

// Container State
import Storage from Storage;
import MyClass from MyClass;

export default new MyClass(new Storage());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment