The comments on && and || using short-circuits and the resultant examples are particularly nice.
This link has been bookmarked by 258 people . It was first bookmarked on 29 Mar 2006, by Dan Phillips.
-
18 Jun 17
kevinoempty
-
14 Aug 14
-
27 Sep 11
-
27 Apr 11
-
26 Jan 11
-
22 Jun 10
-
21 Jan 09
petter wAnother good introduction to JavaScript. Written in a slightly different way. Might work better for some.
-
30 Dec 08
-
13 Oct 08
-
10 Sep 08
-
09 Sep 08
chaldaClanek o syntaxy javascriptu - je tu zminka i o "custom objects", pro me tedy puvodne anonymnich polich :) Dobre shrnuti a opacko, jak na js.
-
04 Sep 08
-
17 Jul 08
Julian KnightNice summary of programming with JS. Assumes a basic programming knowledge so is especially useful for people converting from other languages.
-
Add Sticky Note
-
-
The && and || operators use short-circuit logic, which means they will execute their second operand dependant on the first. This is useful for checking for null objects before accessing their attributes:
var name = o && o.getName();Or for setting default values:
var name = otherName || "default";
-
-
22 May 08
-
06 May 08
-
08 Apr 08
-
26 Mar 08
-
20 Mar 08
-
06 Mar 08
-
03 Mar 08
-
20 Jan 08
-
10 Jan 08
-
06 Jan 08
-
28 Dec 07
-
15 Nov 07
-
13 Nov 07
-
16 Oct 07
-
26 Sep 07
-
13 Sep 07
-
03 Sep 07
danunromantic, practical headsup on the philosophy of javascript's architecture.
cheatsheet presentation language teachmyself javascript howto
-
13 Jul 07
-
30 Jun 07
-
06 May 07
-
04 May 07
-
03 Apr 07
-
16 Mar 07
-
15 Mar 07
-
14 Mar 07
-
12 Mar 07
-
03 Feb 07
-
16 Jan 07
Andre MalheiroNotes made in preparation for a three hour tutorial at ETech in San Diego, March 6th 2006
-
09 Dec 06
-
06 Dec 06
-
02 Dec 06
-
09 Nov 06
-
05 Nov 06
-
29 Oct 06
-
25 Oct 06
-
12 Oct 06
-
09 Oct 06
-
02 Oct 06
-
14 Sep 06
-
02 Sep 06
-
-
An unfortunate side effect of closures is that they make it trivially easy to leak memory in Internet Explorer. JavaScript is a garbage collected language - objects are allocated memory upon their creation and that memory is reclaimed by the browser when no references to an object remain. Objects provided by the host environment are handled by that environment.
Browser hosts need to manage a large number of objects representing the HTML page being presented - the objects of the DOM. It is up to the browser to manage the allocation and recovery of these.
Internet Explorer uses its own garbage collection scheme for this, separate from the mechanism used by JavaScript. It is the interaction between the two that can cause memory leaks.
A memory leak in IE occurs any time a circular reference is formed between a JavaScript object and a native object. Consider the following:
function leakMemory() { var el = document.getElementById('el'); var o = { 'el': el }; el.o = o; }The circular reference formed above creates a memory leak; IE will not free the memory used by el and o until the browser is completely restarted.
The above case is likely to go unnoticed; memory leaks only become a real concern in long running applications or applications that leak large amounts of memory due to large data structures or leak patterns within loops.
Leaks are rarely this obvious - often the leaked data structure can have many layers of references, obscuring the circular reference.
Closures make it easy to create a memory leak without meaning to. Consider this:
function addHandler() { var el = document.getElementById('el'); el.onclick = function() { this.style.backgroundColor = 'red'; } }The above code sets up the element to turn red when it is clicked. It also creates a memory leak. Why? Because the reference to el is inadvertently caught in the closure created for the anonymous inner function. This creates a circular reference between a JavaScript object (the function) and a native object (el).
There are a number of workarounds for this problem. The simplest is this:
function addHandler() { var el = document.getElementById('el'); el.onclick = function() { this.style.backgroundColor = 'red'; } el = null; }This works by breaking the circular reference.
Surprisingly, one trick for breaking circular references introduced by a closure is to add another closure:
function addHandler() { var clickHandler = function() { this.style.backgroundColor = 'red'; } (function() { var el = document.getElementById('el'); el.onclick = clickHandler; })(); }The inner function is executed straight away, and hides its contents from the closure created with clickHandler.
Another good trick for avoiding closures is breaking circular references during the window.onunload event. Many event libraries will do this for you.
-
-
01 Sep 06
-
28 Aug 06
-
14 Aug 06
-
11 Aug 06
-
08 Aug 06
Caspar Van der LindenAn overview of Javascript explaining ways of doing things in a new (object oriented) manner. Good for oldskool people.
-
Why a re-introduction? Because JavaScript has a reasonable claim to being the world's most misunderstood programming language. While often derided as a toy, beneath its deceptive simplicity lie some powerful language features. The last year has seen the launch of a number of high profile JavaScript applications, showing that deeper knowledge of this technology is an important skill for any web developer.
-
-
05 Aug 06
-
03 Aug 06
-
02 Aug 06
-
30 Jul 06
-
09 Jul 06
-
21 Jun 06
doubledayFlickr slides: http://simon.incutio.com/slides/2006/etech/javascript/js-reintroduction-notes.html
-
03 Jun 06
Fogday StudiosWhy a re-introduction? Because JavaScript has a reasonable claim to being the world's most misunderstood programming language. While often derided as a toy, beneath its deceptive simplicity lie some powerful language features. The last year has seen the l
-
31 May 06
Hans Henderson"A good, chatty, easy-to-read introduction to JavaScript with enough detail to be practical" jeremyruston
-
30 May 06
-
28 May 06
-
25 May 06
-
13 May 06
Andrew WhiteWhy a re-introduction? Because JavaScript has a reasonable claim to being the world's most misunderstood programming language. While often derided as a toy, beneath its deceptive simplicity lie some powerful language features. The last year has seen the l
-
27 Apr 06
-
21 Apr 06
-
18 Apr 06
-
04 Apr 06
-
01 Apr 06
-
29 Mar 06
-
pvav pvavNejaký ľahký úvod do Javascriptu od Simona Willisona (prednesený ako 3-hodinový tutoriál na ETechu v San Diego - 6. marec 2006).
-
28 Mar 06
-
27 Mar 06
-
24 Mar 06
-
23 Mar 06
-
22 Mar 06
Public Stiky Notes
Page Comments
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.