473,322 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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:
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
9 2344
Dormilich
8,658 Expert Mod 8TB
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
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
5,390 Expert Mod 4TB
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
im doing the same but as i said i got "prototype is read-only" error.
Aug 5 '09 #5
gits
5,390 Expert Mod 4TB
what's your environment ... the shown way is the 'classical' way to do it.
Aug 5 '09 #6
@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
5,390 Expert Mod 4TB
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
thanks gits.
all works fine now :)
Aug 12 '09 #9
gits
5,390 Expert Mod 4TB
no problem ... just post back to the forum in case you have more questions :)

kind regards
Aug 13 '09 #10

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

Similar topics

25
by: Martin Walke | last post by:
Hi, This may seem like a client side problem but.... I have web pages that are made up of a number of javascript include files as well as 'in page' script. They all work fine running locally...
1
by: jarit | last post by:
Hi, Found these coding guidelines for C#, HTML, Javascript, Java, HTML, PL/SQL, T-SQL, VB and VBScript. Well written and free to download. www.demachina.com/products/swat Jeroen
8
by: chrisdude911 | last post by:
how do i add video into a javascript web page with my own custom buttons?
13
by: paragpdoke | last post by:
Hello Everyone. Merry Christmas to all ! I'm a JavaScript newbie (and new to thescripts.com too). And I have problems in working with objects in JavaScript. The problem: <a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.