views:

135

answers:

4

Hello all,

I have a JS script, in this JS script I have 1 functions which creates an Object or an instance.

Is the object/instance created when the page loads or is it created when I call the function?

If the latter, how can I make it so that it creates the instance when I want it to?

Apologies for a nooby question.

Thanks all

Update

<input type='button' name='submit' onclick='videoUpload();' value='Upload' />

Here is the function:

function videoUpload(){
    $.get("getUploader.php", function(data){
    var viduploader = new AjaxUpload('#vidup', {
       action: data,
       name: 'userfile',
       responseType: 'xml',
       onSubmit: function() {
         // allow only 1 upload
      alert('onSubmit' + data);
         this.disable();
       },
       onComplete: function(file, response){
        alert('Response' + response);
       }
     });
    });
}
A: 

Dependents on where exactly you are doing this. Basically it creates while your calling the function. But if you made this call global it will be created after page will be loaded. And if you want to check whenever it's crated just check for:

if ( typeof( obj) === 'undefined')
{
   // wasn't created or even declared
}

if ( obj === null)
{
  // was declared, but not created
}

PS. I think you need to specify you question a little bit.

After UPDATE:

Actually in this case there is no object created, it will cause the function call each time user will click on the button.

Artem Barger
I make this call using a onclick event. I have updated question.
Abs
+1  A: 

There are a couple ways to do that, I couldn't tell you if one is particularly better than the other, but here are a few ways.

//create a function
function SomeObject() {
    var self = this;
}

//and create it using 'new'
var a = new SomeObject();


//create an object of a function
var SomeOtherObject = function() {
    var self = this;
};

//and in similar fashion
var b = new SomeOtherObject();

//or just create a function that creates new object
var YetAnother = function() {
    var gen = {};
    return gen;
};

//and create by calling the function
var c = YetAnother();
Hugoware
A: 

well an object and an instance are really two different things.

The instance is the living, breathing, "out there and doing it" version of the object.

You create an object, but to use it you have to create an instance of that object. for example, an Array is an object and use use an array like this...

var myArray = new Array();

which creates the 'instance' of that object.

Javascript looks and feels a little bit weird to me when it comes to OOP, but if you've created an object it would look like this

function MyObject(){
    // your objects code goes in here
}

and you would create an instance of that object whenever you write something like this

var myInstance = new MyObject();

Here's a great primer on OOP in javascript. I highly recommend it.

gargantaun
Thanks for the ref material: But do you think in this case the AjaxUpload instance created too on page load? I have updated question.
Abs
The OBJECT will be created when the the page loads and the browser has interpreted the Javascript file. BUT it will only be used when it's called. So in your example, an INSTANCE of AjaxUpload is created every time the button is clicked and the VideoUpload function is called.Before you call VideoUpload, javascript simply knows that an AjaxUpload object exists, and it waits to be told to create an instance of it.
gargantaun
+1  A: 

It depends on how you call the function and if there is a var or not.

Reading from these two webpages will teach you most of what you need to know about javascript.
https://developer.mozilla.org/en/a_re-introduction_to_javascript
http://www.hunlock.com/blogs/Mastering_Javascript_Arrays

You videoUpload functions variables are created when it's called.
So is the AjaxUpload instance created too on page load?
Abs
Put alert( "Created" ); before the "var viduploader" statement, to help clear your mind.AjaxUpload is already defined. But at the moment you call "new AjaxUpload", an instance of it is created. So when videoUpLoad() is called then viduploader is made.