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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.