By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,472 Members | 1,158 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,472 IT Pros & Developers. It's quick & easy.

javascript using object between js files.

P: 13
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:
Expand|Select|Wrap|Line Numbers
  1. var objBall = new Image("abc.gif", 0, 0, visible);
  2. objBall.Move(50,50);
image.js

Expand|Select|Wrap|Line Numbers
  1. function ImageDeneme(path, x, y, visible)
  2. {
  3.     this.image = document.createElement("img");
  4.     this.image.src = path;
  5.     document.body.appendChild(this.image);
  6.     this.Move = Move;
  7.  
  8.     return this.image;
  9. }
  10.  
  11. function Move(x, y)
  12. {
  13.     this.image.style.left = x + "px";
  14.     this.image.style.top = y + "px";
  15. }
waiting for your answers :)
Aug 5 '09 #1
Share this Question
Share on Google+
9 Replies


Dormilich
Expert Mod 5K+
P: 8,639
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:
Expand|Select|Wrap|Line Numbers
  1. ImageDeneme.prototype.Move = function(x, y) {
  2.   // code comes here
  3. }
Aug 5 '09 #2

P: 13
acctually i handle that problem and it works now but i have problem with inheritance now.

ImageClass.prototype = new Drawable(...);
ImageClass.constructor = 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?
Aug 5 '09 #3

gits
Expert Mod 5K+
P: 5,390
here is a very basic example:

Expand|Select|Wrap|Line Numbers
  1. // superclass constructor
  2. function OBJ() {
  3. }
  4.  
  5. // a method
  6. OBJ.prototype.fooMethod = function() {
  7.     alert('OBJs fooMethod');
  8. }
  9.  
  10. // childclass constructor
  11. function myOBJ() {
  12. }
  13.  
  14. // inherit from OBJ
  15. myOBJ.prototype = new OBJ;
  16.  
  17. // fix the constructor
  18. myOBJ.prototype.constructor = myOBJ;
  19.  
  20. // create a instance of myOBJ
  21. var o = new myOBJ;
  22.  
  23. // call the inherited method
  24. o.fooMethod();
  25.  
kind regards
Aug 5 '09 #4

P: 13
im doing the same but as i said i got "prototype is read-only" error.
Aug 5 '09 #5

gits
Expert Mod 5K+
P: 5,390
what's your environment ... the shown way is the 'classical' way to do it.
Aug 5 '09 #6

P: 13
@gits your method is working but i couldnt find how to call base constructor.

Expand|Select|Wrap|Line Numbers
  1. function Drawable(dElement)
  2. {
  3.     this.element = dElement;
  4.     ....
  5. }
  6.  
Expand|Select|Wrap|Line Numbers
  1. Image.prototype = new Drawable;
  2. Image.prototype.constructor = Image
  3.  
  4. function Image(path, x, y, visible)
  5. {    
  6.     debugtext("ImageDeneme");
  7.     this.image= document.createElement("img");
  8.     this.image.src = path;
  9.     this.image.style.position = "absolute";
  10.  
  11.     //Drawable.call(this,this.image);  //I thought it should work but it didnt.
  12.  
  13.     document.body.appendChild(this.element);
  14. };
  15.  
i wonder how to call base class' constructor.
Aug 11 '09 #7

gits
Expert Mod 5K+
P: 5,390
here the short example for the base-constructor call:

Expand|Select|Wrap|Line Numbers
  1. // superclass constructor
  2. function OBJ(p) {
  3.    this.bar = p;
  4. }
  5.  
  6. // a method
  7. OBJ.prototype.fooMethod = function() {
  8.    alert('OBJs fooMethod');
  9. }
  10.  
  11. // childclass constructor
  12. function myOBJ() {
  13.    OBJ.prototype.constructor.apply(this, arguments);
  14. }
  15.  
  16. // inherit from OBJ
  17. myOBJ.prototype = new OBJ;
  18.  
  19. // fix the constructor
  20. myOBJ.prototype.constructor = myOBJ;
  21.  
  22. // create a instance of myOBJ
  23. var o = new myOBJ('foo');
  24.  
  25. alert(o.bar);
  26.  
Aug 12 '09 #8

P: 13
thanks gits.
all works fine now :)
Aug 12 '09 #9

gits
Expert Mod 5K+
P: 5,390
no problem ... just post back to the forum in case you have more questions :)

kind regards
Aug 13 '09 #10

Post your reply

Sign in to post your reply or Sign up for a free account.