Computer not recognizing "Image" object!


#1

I’m trying to declare a new image using this code:

var image = new Image(100, 200);

but Cloud9 says “‘Image’ is not defined”. The think is, Cloud9 CAN tell me what parameters it has and what kind of object it is, but for some reason, it does not recognize the function itself. Am I doing something wrong?

My workspace is public on this link:

https://ide.c9.io/ptolemy2002/canvas-library

inside of Canvas.js.


#2

Unfortunately, IDEs aren’t as smart as humans. In your case, Cloud9 suggested Image() because it knows that some library, somewhere, or maybe some language has that function. IDEs try their best to suggest code that works in the context of your project, but they cannot tell whether a library is loaded or not. For instance, if you go into a new JS file and try writing jQuery, Cloud9 will suggest jQuery code to you, even if you haven’t loaded jQuery anywhere. It assumes that you will load jQuery before trying to execute your code, and this can sometimes be confusing to the programmer. In this case, you may have loaded a library with the Image function, but Cloud9 doesn’t know whether you have or not, so it suggests it to you, but also warns you that it might not actually exist. If you are using a library with the Image() function, you can either ignore it, or add /* global Image */ to the top of the file, which will tell Cloud9 that the function does exist.

If you didn’t load a library with the Image() function, you can either find one that does what you’re looking for, or figure out another way to create the image.


#3

This reply does fix my problem, but when I added jQuery to my project, the main “canvas” object wasn’t working. The console log said, “Uncaught TypeError: this.lineIsColliding is not a function at HTMLCanvasElement.showCoordinates (Canvas.js:40)”

This error consisted even when I changed the name of my Canvas Object to “canvasObj”.

What makes it even more mysterious is that every other function in the object is working perfectly. Also, wouldn’t Cloud9 tell me if a function was not defined when I called it?