javascript using object between js files.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • genesistr
    New Member
    • Aug 2009
    • 13

    #1

    javascript using object between js files.

    Hi,
    I have Image.js and Main.js files. I create image object like test = new Image(...);
    and it can be seen on screen but when i try to do call Move method like
    test.Move(x,y); it crashes. Is there anyway to do that?

    My code :

    main.js:
    Code:
    var objBall = new Image("abc.gif", 0, 0, visible);
    objBall.Move(50,50);
    image.js

    Code:
    function ImageDeneme(path, x, y, visible)
    {
        this.image = document.createElement("img");
        this.image.src = path;
        document.body.appendChild(this.image);
        this.Move = Move;
        
        return this.image;
    }
    
    function Move(x, y)
    {
        this.image.style.left = x + "px";
        this.image.style.top = y + "px";
    }
    waiting for your answers :)
    Last edited by Dormilich; Aug 5 '09, 02:57 PM. Reason: please use [code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I'd expected it to crash on new Image() (ther is a built in image object which you call with the wrong parameters.

    further, the Move() function is not part of the Image object.

    currently I'm not sure, whether this will work since the shown code either doesn't make sense or doesn't seem likely to work together.

    and while we're at OOP, in Javascript methods are better defined through prototyping:
    Code:
    ImageDeneme.prototype.Move = function(x, y) {
      // code comes here
    }

    Comment

    • genesistr
      New Member
      • Aug 2009
      • 13

      #3
      acctually i handle that problem and it works now but i have problem with inheritance now.

      ImageClass.prot otype = new Drawable(...);
      ImageClass.cons tructor = ImageClass;

      but i got "prototype is read-only" error. I'm working on a different platform but it supports javascript and as i got, it has different version of js.

      could you tell how to inherit from a super class?

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        here is a very basic example:

        Code:
        // superclass constructor
        function OBJ() {
        }
        
        // a method
        OBJ.prototype.fooMethod = function() {
            alert('OBJs fooMethod');
        }
        
        // childclass constructor
        function myOBJ() {
        }
        
        // inherit from OBJ
        myOBJ.prototype = new OBJ;
        
        // fix the constructor
        myOBJ.prototype.constructor = myOBJ;
        
        // create a instance of myOBJ
        var o = new myOBJ;
        
        // call the inherited method
        o.fooMethod();
        kind regards

        Comment

        • genesistr
          New Member
          • Aug 2009
          • 13

          #5
          im doing the same but as i said i got "prototype is read-only" error.

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            what's your environment ... the shown way is the 'classical' way to do it.

            Comment

            • genesistr
              New Member
              • Aug 2009
              • 13

              #7
              @gits your method is working but i couldnt find how to call base constructor.

              Code:
              function Drawable(dElement)
              {
                  this.element = dElement;
                  ....
              }
              Code:
              Image.prototype = new Drawable;
              Image.prototype.constructor = Image
              
              function Image(path, x, y, visible)
              {    
                  debugtext("ImageDeneme");
                  this.image= document.createElement("img");
                  this.image.src = path;
                  this.image.style.position = "absolute";
              
                  //Drawable.call(this,this.image);  //I thought it should work but it didnt.
               
                  document.body.appendChild(this.element);
              };
              i wonder how to call base class' constructor.

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5390

                #8
                here the short example for the base-constructor call:

                Code:
                // superclass constructor
                function OBJ(p) {
                   this.bar = p;
                }
                
                // a method
                OBJ.prototype.fooMethod = function() {
                   alert('OBJs fooMethod');
                }
                
                // childclass constructor
                function myOBJ() {
                   OBJ.prototype.constructor.apply(this, arguments);
                }
                
                // inherit from OBJ
                myOBJ.prototype = new OBJ;
                
                // fix the constructor
                myOBJ.prototype.constructor = myOBJ;
                
                // create a instance of myOBJ
                var o = new myOBJ('foo');
                
                alert(o.bar);

                Comment

                • genesistr
                  New Member
                  • Aug 2009
                  • 13

                  #9
                  thanks gits.
                  all works fine now :)

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5390

                    #10
                    no problem ... just post back to the forum in case you have more questions :)

                    kind regards

                    Comment

                    Working...