December 19th, 2013, 04:41 PM
-
Object Initialize
Why we always intiate Object in javascript ?
like
var preloadimages = new Image();
to access its properties?
if we want to load all images in browser cache ... so we still need to initialize the object??
December 19th, 2013, 05:25 PM
-
Um, how do you wanna access an object without creating it first? It simply does not exist until you tell JavaScript to create it.
There are different techniques of preloading images. But I can't comment on that until you've explained the concrete use case.
Comments on this post
December 20th, 2013, 02:48 PM
-
and the reason for initializing an object?? what is the specific reason for it???? I know everything is object in javascript
December 20th, 2013, 03:06 PM
-
You're confused.
Objects don't just come from nowhere. They need to be created. JavaScript has to write a bunch of bits representing the object into memory. Prior to that, there is no object.
Some objects are automatically created by JavaScript when it starts running (DOM nodes etc.). All other objects have to be explicitly created by you.
It works just like in the "real world". There is no car until somebody builds it.
Originally Posted by ionezation
I know everything is object in javascript
That's an oversimplification which won't help you understand JavaScript. Better forget it.
December 20th, 2013, 03:14 PM
-
Originally Posted by ionezation
and the reason for initializing an object?? what is the specific reason for it???? I know everything is object in javascript
You should see Object like a empty container.
As Jacques1 also tried to say, you need to initialize preloadimages so the the computer know you want to work with images.
December 20th, 2013, 03:36 PM
-
Originally Posted by ionezation
Why we always intiate Object in javascript ?
like
var preloadimages = new Image();
to access its properties?
if we want to load all images in browser cache ... so we still need to initialize the object??
If you run the code below, you'll notice that the only code that executes are ones that appear after the objects they refer to. The first two alerts cannot execute because there are no objects for them to refer to. (Remember: javascript is an interpretive language, which means it tries to execute each line as it comes to it)
Code:
<!DOCTYPE html>
<html>
<script type="text/javascript">
var divObj = document.getElementById('div1');
var x = document.images[0].src;
alert(1 + divObj)
alert(2 + x)
</script>
<body>
<div id="div1"><img src="somePic.jpg"/></div>
<script type="text/javascript">
var divObj = document.getElementById('div1');
alert(3 + divObj)
var x = document.images[0].src;
alert(4 + x)
</script>
</body>
</html>
December 21st, 2013, 07:50 AM
-
December 21st, 2013, 08:51 AM
-
Originally Posted by ionezation
hmm i got it !
glad
just keep asking....that's how we all learn
December 25th, 2013, 11:14 AM
-
now i got it ... when we have to do something we create an object first then we take all the properties and methods from it to the TAG we want to target ?? RIGHT??
December 25th, 2013, 04:03 PM
-
I think you are right.
But depend what what you mean with "TAG"?
Another word for object?
December 28th, 2013, 03:09 PM
-
ohh sorry about that TAG i meant html element