Mostly Bollogs, I'm afraid

But occasionally, a glimmer of truth.
If you find one, please let me know.



Thursday 16 February 2012

javascript

7. What possibly unintentional behaviour is exhibited in the following code and how would you correct it?

function buildItems() { var i, arr = []; for (i = 0; i < 20; i++) {
arr.push({ item: create(i), kill: function() {
remove(i); }
}); } return arr;
} buildItems()[10].kill();

4 comments:

Anonymous said...

'Kill function'? Wasn't that in Robocop?

Adam Feather said...
This comment has been removed by the author.
Adam Feather said...

The final call "buildItems()[10].kill();" will call the remove function with "20" as its parameter, not "10" as you might expect.

Javascript Countdown Timer said...

It is entirely possible to EDIT markup in a textarea. The behavior of HTML entities within a textarea allows for this behavior, but it must be done correctly to be valid. Server-Side Navigation Bar and is the fancy name given to the ring navigation code that is called by a piece of javascript. It is on the page where you copy this piece of javascript that I had the problem of it rendering rather than showing in the textarea.

Cheers!!