PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 26, 2022

[FIXED] How to access JavaScript module pattern in my case?

 November 26, 2022     javascript, module     No comments   

Issue

I have defined the following module in MyModule.js:

MyModule.js:

MyModule = {

    controller: {
       paintCar: function(color){
            //PAINTING PROCESS
       }
    },

    tester: {
        ...
    },

};

Then, I have another javascript file, other.js:

other.js:

MyModule.controller.paintCar('red');

My index.html

<body>
    <script scr="MyModule.js"></script>
    <script src="other.js"></script>
  </body>

All these files are put under WebContent directory of Eclipse Dynamic Web Project.

In other.js when I try to run the above code, I got error "MyModule is not defined". Why?


Solution

You misspelt src here: <script scr="MyModule.js">. This would have been picked up by basic automated QA testing.

(Original answer before the theory was confirmed) Presumably, because you aren't loading the script files properly. Since you haven't shown us the code that does that (or even told us what environment you are using) it is hard to say exactly how you went wrong.

Assuming you are using client side JS in a webpage, your code should look something like:

<script src="MyModule.js"></script>
<script src="other.js"></script>

Order is significant. End tags are mandatory. Self-closing tag syntax is unacceptable (unless the document is served as application/xhtml+xml)

(This uses HTML 5 syntax. For HTML 4 / XHTML 1, add a type attribute. For HTML 3.2, add a language attribute)



Answered By - Quentin
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing