Advanced Search

Results 1 to 3 of 3

Thread: JS OOP method with parameter??

  1. #1
    Join Date
    Aug 2011
    Location
    both, Germany and Bulgaria
    Posts
    54
    Thanks
    14
    Thanked 10 Times in 10 Posts

    Question JS OOP method with parameter??

    Hi guys,

    there is a roughly Background object definition with 4 layers like:
    Code:
    function Background(){
    
       this.layer1 = new Layer(1);
       this.layer2 = new Layer(2);
       this.layer3 = new Layer(3);
       this.layer4 = new Layer(4);
    
    }
    
    function Layer(number){
    
       this.number = number;
       this.image = ''; // Image file name is empty for now, I'd like to fill it out appropriate later
       ... more misc stuff ...
    
    }
    Let's say I'd like to define an additional method to the object Background, which takes a layer and does something to it, i.e. add a picture to the layer.
    I've tried with following definition without success; FF25 did it, but IE10 crashes every time?!

    Code:
    function Background(){
    
       this.layer1 = new Layer(1);
       this.layer2 = new Layer(2);
       this.layer3 = new Layer(3);
       this.layer4 = new Layer(4);
    
       this.addimage = function(layer){
    	layer.image = 'picture' + layer.number + '.jpg';
       }
    }
    
    ...
    
    // ... and now I'd like to use my method
    
    var background = new Background();
    background.addimage(background.layer1);
    Is there an error?!
    Last edited by XManBG; 11-17-2013 at 01:31 PM.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,672
    Thanks
    43
    Thanked 3,125 Times in 3,091 Posts
    Blog Entries
    12

    Default

    I have IE 11. But in its IE 10 mode I have no problem:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    function Background(){
    
       this.layer1 = new Layer(1);
       this.layer2 = new Layer(2);
       this.layer3 = new Layer(3);
       this.layer4 = new Layer(4);
       this.addimage = function(layer){
    	layer.image = 'picture' + layer.number + '.jpg';
       };
    }
    
    function Layer(number){
    
       this.number = number;
       this.image = ''; // Image file name is empty for now, I'd like to fill it out appropriate later
       //... more misc stuff ...
    
    }
    var background = new Background();
    background.addimage(background.layer1);
    </script>
    </head>
    <body>
    <script type="text/javascript">
    document.write(background.layer1.image);
    </script>
    </body>
    </html>
    I get:

    picture1.jpg
    The only problem that I can think of is that perhaps 'background' and/or 'Background' are reserved words in the scope. Like if you were doing this in quirks mode perhaps. I used a standards invoking DOCTYPE.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    XManBG (11-17-2013)

  4. #3
    Join Date
    Aug 2011
    Location
    both, Germany and Bulgaria
    Posts
    54
    Thanks
    14
    Thanked 10 Times in 10 Posts

    Default

    Thanks John,

    I was unsure about using of object like parameter.
    On your suggestion I've changed all names to something not very usual and the problem was still there.

    A little bit later, I realized that it was something inside my '//... more misc stuff ...' where I had some calculations with the picture size.
    The problem was, that the calculations were performed before the pictures were really loaded. Sometimes the loading was quicker, and it functioned, but sometimes not, and it crashed.
    After upgrading all picture loads with image.onload events, everything went just fine.

    Cheers

Similar Threads

  1. Resolved echo method + static method
    By ggalan in forum PHP
    Replies: 1
    Last Post: 05-29-2011, 04:56 AM
  2. What is the 3rd parameter?
    By kuau in forum PHP
    Replies: 2
    Last Post: 11-07-2008, 11:08 PM
  3. Using the REV parameter?
    By yabot in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 03-12-2008, 10:31 PM
  4. Using a function as a parameter of another one
    By ellyz in forum JavaScript
    Replies: 3
    Last Post: 12-27-2005, 09:20 PM
  5. Using a function as a parameter of another one
    By ellyz in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 12-27-2005, 10:34 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •