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?

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;








-3















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










share|improve this question






















  • 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

















-3















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










share|improve this question






















  • 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













-3












-3








-3








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










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 your books module source, please? Are you saying it contains a class?

    – 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












  • Can you post your books module source, please? Are you saying it contains a class?

    – 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












2 Answers
2






active

oldest

votes


















0














 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
;





share|improve this answer






























    0














    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'
    );





    share|improve this answer























      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
      );



      );













      draft saved

      draft discarded


















      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









      0














       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
      ;





      share|improve this answer



























        0














         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
        ;





        share|improve this answer

























          0












          0








          0







           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
          ;





          share|improve this answer













           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
          ;






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 23 at 12:36









          Jonas WilmsJonas Wilms

          69.7k63764




          69.7k63764























              0














              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'
              );





              share|improve this answer



























                0














                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'
                );





                share|improve this answer

























                  0












                  0








                  0







                  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'
                  );





                  share|improve this answer













                  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'
                  );






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 23 at 12:34









                  BergiBergi

                  386k64601927




                  386k64601927



























                      draft saved

                      draft discarded
















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript