Search

4/19/2009

Douglas Crockford — The JavaScript Programming Language

Douglas Crockford — The JavaScript Programming Language
Unary operator can convert strings to numbers


>>> a = '5'
"5"
>>> alert(+a) // 5
>>> alert(typeof(+a)) // number


Object Augmentation
* New members can be added to any object by simple assignment
* There is no need to define a new class

Arrays
* Array inherits from Object.
* Indexes are converted to strings and used as names for retrieving values.

Functions
* Functions are first-class objects - A first-class object is one that can be dynamically created, destroyed or passed as an argument.
* Functions can be passed, returned, and stored just like any other value
* Functions inherit from Object and can store name/value pairs.
For example, in C and C++, it is not possible to create new functions at runtime (although see anonymous lambda bindings in the Boost library), whereas other kinds of objects can be created at runtime. As a result, functions in C are not first-class objects; instead, they are sometimes called second-class objects because they can still be manipulated in most of the above fashions (via function pointers). Similarly, strings are not first class objects in FORTRAN 66 because it is not possible to assign them to variables (unlike, for example, numbers).

Scope
* Static scoping or Lexical scoping - An inner function has access to the variables and parameters of functions that it is contained within.
* [OOJS] Lexical scope - In JavaScript, functions have lexical scope. This means that functions create their environment (scope) when they are defined, not when they are executed
* [rhino 8.8.1] Functions in JavaScript are lexically rather than dynamically scoped. This means that they run in the scope in which they are defined, not the scope from which they are executed. When a function is defined, the current scope chain is saved and becomes part of the internal state of the function. At the top level, the scope chain simply consists of the global object, and lexical scoping is not particularly relevant. When you define a nested function, however, the scope chain includes the containing function. This means that nested functions can access all of the arguments and local variables of the containing function.

supplant

String.prototype.supplant = function (o) {
return this.replace(/{([^{}]*)}/g,
function (a, b) {
var r = o[b];
return typeof r === 'string' ?
r : a;
}
);
};


Function function
new Function(parameters, body)
* The Function constructor takes zero or more parameter name strings, and a body string, and uses the JavaScript compiler to produce a function object.
* It should only be used to compile fresh source from a server.


var object = function(o) {
var F = function(){};
F.prototype = o;
return new F();
}

var foo = {'foo': 'foo'}
var bar = object(foo);
bar.info = 'hello world';
bar.sayhello = function(){alert(this.info)}


Threads

* The language definition is neutral on threads
* Some language processors (like SpiderMonkey) provide thread support
* Threads are evil

沒有留言: