making modular javascript library by importing class methods without need to the whole classexport / import single class method using ES6 modules?How can I force clients to refresh JavaScript files?What techniques can be used to define a class in JavaScript, and what are their trade-offs?Change the selected value of a drop-down list with jQueryHow to insert an element after another element in JavaScript without using a library?How to decide when to use Node.js?How to make a JSONP request from Javascript without JQuery?Using Node.JS, how do I read a JSON file into (server) memory?How does Access-Control-Allow-Origin header work?Managing jQuery plugin dependency in webpackWhy is immutability so important (or needed) in JavaScript?
Multi tool use
What's special about a Bunsen burner?
Who was this character from the Tomb of Annihilation adventure before they became a monster?
Should these notes be played as a chord or one after another?
Why doesn't Rocket Lab use a solid stage?
Is the schwa sound consistent?
How does Howard Stark know this?
Was there ever any real use for a 6800-based Apple I?
Do atomic orbitals "pulse" in time?
Why was Thor doubtful about his worthiness to Mjolnir?
What is the best way for a skeleton to impersonate human without using magic?
Can 'sudo apt-get remove [write]' destroy my Ubuntu?
Make all the squares explode
Is there a spell to protect inanimate objects?
Light Switch Terminals
Would an 8% reduction in drag outweigh the weight addition from this custom CFD-tested winglet?
What does a comma mean inside an 'if' statement?
Understanding integration over Orthogonal Group
Drawing lines to nearest point
What is the significance of 4200 BCE in context of farming replacing foraging in Europe?
What are the components of a legend (in the sense of a tale, not a figure legend)?
Extrude the faces of a cube symmetrically along XYZ
Extracting sublists that contain similar elements
Why does the Earth follow an elliptical trajectory rather than a parabolic one?
Why not just directly invest in the holdings of an ETF?
making modular javascript library by importing class methods without need to the whole class
export / import single class method using ES6 modules?How can I force clients to refresh JavaScript files?What techniques can be used to define a class in JavaScript, and what are their trade-offs?Change the selected value of a drop-down list with jQueryHow to insert an element after another element in JavaScript without using a library?How to decide when to use Node.js?How to make a JSONP request from Javascript without JQuery?Using Node.JS, how do I read a JSON file into (server) memory?How does Access-Control-Allow-Origin header work?Managing jQuery plugin dependency in webpackWhy is immutability so important (or needed) in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm trying to make a reusable javascript library for example books class and have createbook, checkforbook, modify book methods
I want to use it like so
import Library, checkForBook, modifyBook from 'books'
Library.createBook(
name : 'firstbook',
year: 2012
)
checkForBook('firstBook')
modifyBook('firstBook',
name: 'secondBook'
)
my problem right now is: how to do it without need to use new keywrod , and how to use methods wihout need to make it like Library.checkForBook
and still have access to the list of books
javascript
add a comment |
I'm trying to make a reusable javascript library for example books class and have createbook, checkforbook, modify book methods
I want to use it like so
import Library, checkForBook, modifyBook from 'books'
Library.createBook(
name : 'firstbook',
year: 2012
)
checkForBook('firstBook')
modifyBook('firstBook',
name: 'secondBook'
)
my problem right now is: how to do it without need to use new keywrod , and how to use methods wihout need to make it like Library.checkForBook
and still have access to the list of books
javascript
Add your books script and the current issue that you're facing. Here is a similar question stackoverflow.com/questions/38308307/…
– Harshana
Mar 23 at 12:19
Can you post yourbooks
module source, please? Are you saying it contains aclass
?
– Bergi
Mar 23 at 12:30
add a comment |
I'm trying to make a reusable javascript library for example books class and have createbook, checkforbook, modify book methods
I want to use it like so
import Library, checkForBook, modifyBook from 'books'
Library.createBook(
name : 'firstbook',
year: 2012
)
checkForBook('firstBook')
modifyBook('firstBook',
name: 'secondBook'
)
my problem right now is: how to do it without need to use new keywrod , and how to use methods wihout need to make it like Library.checkForBook
and still have access to the list of books
javascript
I'm trying to make a reusable javascript library for example books class and have createbook, checkforbook, modify book methods
I want to use it like so
import Library, checkForBook, modifyBook from 'books'
Library.createBook(
name : 'firstbook',
year: 2012
)
checkForBook('firstBook')
modifyBook('firstBook',
name: 'secondBook'
)
my problem right now is: how to do it without need to use new keywrod , and how to use methods wihout need to make it like Library.checkForBook
and still have access to the list of books
javascript
javascript
asked Mar 23 at 12:01
mr devmr dev
31
31
Add your books script and the current issue that you're facing. Here is a similar question stackoverflow.com/questions/38308307/…
– Harshana
Mar 23 at 12:19
Can you post yourbooks
module source, please? Are you saying it contains aclass
?
– Bergi
Mar 23 at 12:30
add a comment |
Add your books script and the current issue that you're facing. Here is a similar question stackoverflow.com/questions/38308307/…
– Harshana
Mar 23 at 12:19
Can you post yourbooks
module source, please? Are you saying it contains aclass
?
– Bergi
Mar 23 at 12:30
Add your books script and the current issue that you're facing. Here is a similar question stackoverflow.com/questions/38308307/…
– Harshana
Mar 23 at 12:19
Add your books script and the current issue that you're facing. Here is a similar question stackoverflow.com/questions/38308307/…
– Harshana
Mar 23 at 12:19
Can you post your
books
module source, please? Are you saying it contains a class
?– Bergi
Mar 23 at 12:30
Can you post your
books
module source, please? Are you saying it contains a class
?– Bergi
Mar 23 at 12:30
add a comment |
2 Answers
2
active
oldest
votes
const books = new Map(); // store the books somehow
export function createBook( name, year ) // make them available as import createBook from ...
const book = name, year ;
books.set(name, book);
return book;
export function checkForBook(name)
return books.has(name);
export function modifyBook(name, update)
if(!checkForBook(name))
throw new Error("Cannot modify, book doesnt exist");
Object.assign(books.get(name), update);
export default // make them available as Library.createBook
createBook,
checkForBook,
modifyBook
;
add a comment |
I think you are looking for
// library.js
export default class Library
…
// books.js
import Library from './library';
// creates a singleton:
export default const books = new Library();
export function checkForBook(name)
return books.checkForBook(name);
export function modifyBook(name, value)
return books.modifyBook(name, value);
// main.js
// imports the singleton, not the `Library` class
import books, checkForBook, modifyBook from 'books';
books.createBook(
name : 'firstbook',
year: 2012
);
checkForBook('firstBook');
modifyBook('firstBook',
name: 'secondBook'
);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55313521%2fmaking-modular-javascript-library-by-importing-class-methods-without-need-to-the%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
const books = new Map(); // store the books somehow
export function createBook( name, year ) // make them available as import createBook from ...
const book = name, year ;
books.set(name, book);
return book;
export function checkForBook(name)
return books.has(name);
export function modifyBook(name, update)
if(!checkForBook(name))
throw new Error("Cannot modify, book doesnt exist");
Object.assign(books.get(name), update);
export default // make them available as Library.createBook
createBook,
checkForBook,
modifyBook
;
add a comment |
const books = new Map(); // store the books somehow
export function createBook( name, year ) // make them available as import createBook from ...
const book = name, year ;
books.set(name, book);
return book;
export function checkForBook(name)
return books.has(name);
export function modifyBook(name, update)
if(!checkForBook(name))
throw new Error("Cannot modify, book doesnt exist");
Object.assign(books.get(name), update);
export default // make them available as Library.createBook
createBook,
checkForBook,
modifyBook
;
add a comment |
const books = new Map(); // store the books somehow
export function createBook( name, year ) // make them available as import createBook from ...
const book = name, year ;
books.set(name, book);
return book;
export function checkForBook(name)
return books.has(name);
export function modifyBook(name, update)
if(!checkForBook(name))
throw new Error("Cannot modify, book doesnt exist");
Object.assign(books.get(name), update);
export default // make them available as Library.createBook
createBook,
checkForBook,
modifyBook
;
const books = new Map(); // store the books somehow
export function createBook( name, year ) // make them available as import createBook from ...
const book = name, year ;
books.set(name, book);
return book;
export function checkForBook(name)
return books.has(name);
export function modifyBook(name, update)
if(!checkForBook(name))
throw new Error("Cannot modify, book doesnt exist");
Object.assign(books.get(name), update);
export default // make them available as Library.createBook
createBook,
checkForBook,
modifyBook
;
answered Mar 23 at 12:36
Jonas WilmsJonas Wilms
69.7k63764
69.7k63764
add a comment |
add a comment |
I think you are looking for
// library.js
export default class Library
…
// books.js
import Library from './library';
// creates a singleton:
export default const books = new Library();
export function checkForBook(name)
return books.checkForBook(name);
export function modifyBook(name, value)
return books.modifyBook(name, value);
// main.js
// imports the singleton, not the `Library` class
import books, checkForBook, modifyBook from 'books';
books.createBook(
name : 'firstbook',
year: 2012
);
checkForBook('firstBook');
modifyBook('firstBook',
name: 'secondBook'
);
add a comment |
I think you are looking for
// library.js
export default class Library
…
// books.js
import Library from './library';
// creates a singleton:
export default const books = new Library();
export function checkForBook(name)
return books.checkForBook(name);
export function modifyBook(name, value)
return books.modifyBook(name, value);
// main.js
// imports the singleton, not the `Library` class
import books, checkForBook, modifyBook from 'books';
books.createBook(
name : 'firstbook',
year: 2012
);
checkForBook('firstBook');
modifyBook('firstBook',
name: 'secondBook'
);
add a comment |
I think you are looking for
// library.js
export default class Library
…
// books.js
import Library from './library';
// creates a singleton:
export default const books = new Library();
export function checkForBook(name)
return books.checkForBook(name);
export function modifyBook(name, value)
return books.modifyBook(name, value);
// main.js
// imports the singleton, not the `Library` class
import books, checkForBook, modifyBook from 'books';
books.createBook(
name : 'firstbook',
year: 2012
);
checkForBook('firstBook');
modifyBook('firstBook',
name: 'secondBook'
);
I think you are looking for
// library.js
export default class Library
…
// books.js
import Library from './library';
// creates a singleton:
export default const books = new Library();
export function checkForBook(name)
return books.checkForBook(name);
export function modifyBook(name, value)
return books.modifyBook(name, value);
// main.js
// imports the singleton, not the `Library` class
import books, checkForBook, modifyBook from 'books';
books.createBook(
name : 'firstbook',
year: 2012
);
checkForBook('firstBook');
modifyBook('firstBook',
name: 'secondBook'
);
answered Mar 23 at 12:34
BergiBergi
386k64601927
386k64601927
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55313521%2fmaking-modular-javascript-library-by-importing-class-methods-without-need-to-the%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3j568MG,7B X4R OSpn 3kd
Add your books script and the current issue that you're facing. Here is a similar question stackoverflow.com/questions/38308307/…
– Harshana
Mar 23 at 12:19
Can you post your
books
module source, please? Are you saying it contains aclass
?– Bergi
Mar 23 at 12:30