Benx Shen's personal annotations on this page
-
It's useful to start with an idea of the language's history. JavaScript was created in 1995 by Brendan Eich, an engineer at Netscape, and first released with Netscape 2 early in 1996. It was originally going to be called LiveScript, but was renamed in an ill-fated marketing decision to try to capitalise on the popularity of Sun Microsystem's Java language — despite the two having very little in common. This has been a source of confusion ever since.
-
var name = o && o.getName();
-
var name = otherName || "default";
-
The first argument to
apply()is the object that should be treated as 'this'. -
apply()has a sister function namedcall, which again lets you set 'this' but takes an expanded argument list as opposed to an array. -
Closures
-
A closure is the combination of a function and the scope object in which it was created.
Closures let you save state - as such, they can often be used in place of objects.
-
An unfortunate side effect of closures is that they make it trivially easy to leak memory in Internet Explorer.
-
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
elis 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). -
delete el;
-
Surprisingly, one trick for breaking circular references introduced by a closure is to add another closure:
-
var clickHandler = function() {
this.style.backgroundColor = 'red';
}
(function() {
var el = document.getElementById('el');
el.onclick = clickHandler;
})();
This link has been bookmarked by 13 people . It was first bookmarked on 27 Nov 2008, by someone privately.
-
Dominique Rose-Rosette"A re-introduction to JavaScript"
-
-
It's useful to start with an idea of the language's history. JavaScript was created in 1995 by Brendan Eich, an engineer at Netscape, and first released with Netscape 2 early in 1996. It was originally going to be called LiveScript, but was renamed in an ill-fated marketing decision to try to capitalise on the popularity of Sun Microsystem's Java language — despite the two having very little in common. This has been a source of confusion ever since.
-
var name = o && o.getName();
- 10 more annotations...
-
-
Rajkumar SinghA re-introduction to JavaScript - MDC
-
-
Whenever JavaScript executes a function, a 'scope' object is created to hold the local variables created within that function. It is initialised with any variables passed in as function parameters. This is similar to the global object that all global variables and functions live in
-
firstly, a brand new scope object is created every time a function starts executing, and secondly, unlike the global object (which in browsers is accessible as window) these scope objects cannot be directly accessed from your JavaScript code.
- 2 more annotations...
-
-
-
That may seem a little silly, but functions have access to an additional variable inside their body called
arguments, which is an array-like object holding all of the values passed to the function. Let's re-write the add function to take as many values as we want:function add() {
var sum = 0;
for (var i = 0, j = arguments.length; i < j; i++) {
sum += arguments[i];
}
return sum;
}
> add(2, 3, 4, 5)
14That's really not any more useful than writing
2 + 3 + 4 + 5though. Let's create an averaging function:function avg() {
var sum = 0;
for (var i = 0, j = arguments.length; i < j; i++) {
sum += arguments[i];
}
return sum / arguments.length;
}
> avg(2, 3, 4, 5)
3.5This is pretty useful, but introduces a new problem. The
avg()function takes a comma separated list of arguments - but what if you want to find the average of an array? You could just rewrite the function as follows:function avgArray(arr) {
var sum = 0;
for (var i = 0, j = arr.length; i < j; i++) {
sum += arr[i];
}
return sum / arr.length;
}
> avgArray([2, 3, 4, 5])
3.5But it would be nice to be able to reuse the function that we've already created. Luckily, JavaScript lets you call a function and call it with an arbitrary array of arguments, using the
apply()method of any function object.> avg.apply(null, [2, 3, 4, 5])
3.5The second argument to
apply()is the array to use as arguments; the first will be discussed later on. This emphasizes the fact that functions are objects too.
-
-
Sarah-Jane BollingThis site is a lot "wordier" than others that I've seen...but what I personally like about it is the examples followed by a short description as to what it is I am looking at. I like that (and learn it easier)
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.