Jump to content

Includes and Objects..


Maknib

Recommended Posts

Hi guys,

 

i am taking the Javascript course from Learnable.com (sitepoint). we have come up to Objects in Javascript.. he has stated something like:

 

var MyObject = 
{
   init:  function(){
      //do stuff
    },
    something:  function(){
      //do something else
    }
};

MyObject.init();

 

he has said that i need to put myObject.init(); on the bottom as the object hasn't been created yet if i try to call it from the top..

 

i'm gathering that this is similar to..

 

public class MyObject{

   public void init(){
      //do stuff
   }


   public void something(){
     //do something else
   }

}

i'm wondering, because i'm starting to get used to working with classes and a Main class... can i have all my objects declared in another JS file, and use an include or an import in another "main" js file? purely for neatness? as i would like to have all funtions / objects in one file and the workings of the script in another.

 

 

is this possible in Javascript?

 

hope this makes sense

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/257427-includes-and-objects/
Share on other sites

is this possible in Javascript?

 

There isn't much that isn't these days. You can include javascript files into your pages using....

 

<script type="javascript" src="somescript.js">

 

So in your case you could have....

 

<script type="javascript" src="yourobjects.js">
<script type="javascript" src="yourcallingcode.js">

 

There are also numerous libraries around that will let you dynamically load js into other js files, but generally, these are overkill for most cases.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.