#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    100
    Rep Power
    0

    Object Initialize


    Why we always intiate Object in javascript ?
    like

    var preloadimages = new Image();

    to access its properties?
    if we want to load all images in browser cache ... so we still need to initialize the object??
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Um, how do you wanna access an object without creating it first? It simply does not exist until you tell JavaScript to create it.

    There are different techniques of preloading images. But I can't comment on that until you've explained the concrete use case.

    Comments on this post

    • richpri agrees : I couldn't figgure out how to reply. I agree with your comment.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    100
    Rep Power
    0
    and the reason for initializing an object?? what is the specific reason for it???? I know everything is object in javascript
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    You're confused.

    Objects don't just come from nowhere. They need to be created. JavaScript has to write a bunch of bits representing the object into memory. Prior to that, there is no object.

    Some objects are automatically created by JavaScript when it starts running (DOM nodes etc.). All other objects have to be explicitly created by you.

    It works just like in the "real world". There is no car until somebody builds it.



    Originally Posted by ionezation
    I know everything is object in javascript
    That's an oversimplification which won't help you understand JavaScript. Better forget it.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,730
    Rep Power
    1959
    Originally Posted by ionezation
    and the reason for initializing an object?? what is the specific reason for it???? I know everything is object in javascript
    You should see Object like a empty container.

    As Jacques1 also tried to say, you need to initialize preloadimages so the the computer know you want to work with images.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Location
    Where the World Once Stood
    Posts
    11
    Rep Power
    0
    Originally Posted by ionezation
    Why we always intiate Object in javascript ?
    like

    var preloadimages = new Image();

    to access its properties?
    if we want to load all images in browser cache ... so we still need to initialize the object??

    If you run the code below, you'll notice that the only code that executes are ones that appear after the objects they refer to. The first two alerts cannot execute because there are no objects for them to refer to. (Remember: javascript is an interpretive language, which means it tries to execute each line as it comes to it)

    Code:
    <!DOCTYPE html>
    <html>
    
    <script type="text/javascript">
    var divObj = document.getElementById('div1');
    var x = document.images[0].src;
    alert(1 + divObj)
    alert(2 + x)
    </script>
    <body>
    <div id="div1"><img src="somePic.jpg"/></div>
    
    <script type="text/javascript">
    var divObj = document.getElementById('div1');
    alert(3 + divObj)
    var x = document.images[0].src;
    alert(4 + x)
    </script>
    </body>
    </html>
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    100
    Rep Power
    0
    hmm i got it !
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Location
    Where the World Once Stood
    Posts
    11
    Rep Power
    0
    Originally Posted by ionezation
    hmm i got it !
    glad just keep asking....that's how we all learn
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    100
    Rep Power
    0
    now i got it ... when we have to do something we create an object first then we take all the properties and methods from it to the TAG we want to target ?? RIGHT??
  18. #10
  19. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,730
    Rep Power
    1959
    I think you are right.

    But depend what what you mean with "TAG"? Another word for object?
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    100
    Rep Power
    0
    ohh sorry about that TAG i meant html element

IMN logo majestic logo threadwatch logo seochat tools logo