In Javascript, by what mechanism does setting an Image src property trigger an image load?
Posted
by
brainjam
on Stack Overflow
See other posts from Stack Overflow
or by brainjam
Published on 2011-01-08T07:59:03Z
Indexed on
2011/01/09
7:54 UTC
Read the original article
Hit count: 260
JavaScript
|properties
One of the things you learn early on when manipulating a DOM using Javascript is the following pattern:
var img = new Image(); // Create new Image object
img.onload = function(){
// execute drawImage statements here
}
img.src = 'myImage.png'; // Set source path
As far as I know, in general when you set an object property there are no side effects. So what is the mechanism for triggering an image load? Is it just magic? Or can I use a similar mechanism to implement a class Foo that supports a parallel pattern?
var foo = new Foo(); // Create new object
foo.barchanged = function(){
// execute something after side effect has completed
}
foo.bar = 'whatever'; // Assign something to 'bar' property
I'm vaguely aware of Javascript getters and setters. Is this how Image.src triggers a load?
© Stack Overflow or respective owner