A-letubby
A-letubby

Reputation: 9142

Many new instances of requirejs module

I am creating a typing game which I have "app.js" as a main and loading "words.js" by requirejs.

I need to use > 2 words but I am still naive with javascript and not sure this is right to do in AMD. Anyone could point me out. I would really appreciate it.

I think it would be like following code but it doesn't work and give me error

"Uncaught TypeError: object is not a function"

[app.js]

require(['jquery','app/canvas','app/words'], function($,game_screen,words){
   var words1 = new words();
   var words2 = new words();
   .
   .
});

[words.js]

define(['app/canvas'],function(canvas){
   var word_list=[{"word1"},{"word2"},...];
   return {
        addWord: function(new_word){
             .
             .
});

Upvotes: 0

Views: 1905

Answers (1)

Herman Tran
Herman Tran

Reputation: 1591

Right now you're returning an object from your words module: { addWord: function() {} ... }. So in app.js, when you set words to be equal to the object returned from the words module, you would invoke the functions by doing words.addWord().

If instead you want to use the new words() syntax in app.js, you would have to change your words module to return a function instead of an object (hence the error):

define(['app/canvas'],function(canvas) {
   var word_list=[{"word1"},{"word2"},...]; // not sure how this is used

   function words() {
     // some code
   }

   words.prototype.addWords = function() {
     // some code
   }

   return words;
}

Upvotes: 4

Related Questions