Skip to content

Instantly share code, notes, and snippets.

@dominictarr
Created June 10, 2012 06:59
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dominictarr/2904285 to your computer and use it in GitHub Desktop.
Save dominictarr/2904285 to your computer and use it in GitHub Desktop.

why javascript is so great

JavaScript is great because it is so simple.

How is it so simple? well, because it isn't needlessly complex!

A great example is that nested scopes, prototype chains, and node modules, all work in a precisely similar way.

If you refer to a variable in a nested scope, the interpereter checks if it is defined in that scope, then in the outer scope, recursively.

see example

This makes it easy to shadow data.

Prototype chains work exactly the same!

If you refer to a property of an object, the interpereter checks if it is defined on that object, then on the prototype object, recursively.

see example

Those two sentences where very similar!

Scopes differ from prototypes because you can lengthen the prototype chain after the fact with prototypes, but in scopes the structure of the scope-chain is hard-coded.

Now, with modules:

If you require a (non-relative) module X, the interpereter checks if it is defined in ./node_modules/X, then in parent directories ../node_modules/X, recursively.

see example

In most languages, these are three different areas that you need to understand seperately. In nodejs/javascript, although the surface features differ, the core rules that these follow are identical.

That makes for a simple, powerful language.

module.exports = 'pine-apple'
module.exports = 'apple'
require('A')
require('B')
require('C')
require('D')
module.exports = 'saussage'
module.exports = 'banana'
module.exports = 'bacon'
module.exports = 'hamburger'
console.log(require('./package').name, require('a'), require('b'), require('c'))
console.log(require('./package').name, require('a'), require('b'), require('c'))
console.log(require('./package').name, require('a'), require('b'), require('c'))
console.log(require('./package').name, require('a'), require('b'), require('c'))
node_modules/A/node_modules/B/node_modules/C/all.js
{"name": "C"}
{"name": "B"}
{"name": "D"}
{"name": "A"}
var inherits = require('util').inherits
function A () {}
A.prototype.a = 'apple'
A.prototype.b = 'banana'
A.prototype.c = 'hamburger'
inherits(B, A)
function B () {}
B.prototype.a = 'pine-apple'
inherits(C, B)
function C () {}
C.prototype.b = 'saussage'
inherits(D, A)
function D () {}
D.prototype.c = 'bacon'
function print (e) {
console.log(e.constructor.name, e.a, e.b, e.c)
}
print(new A())
print(new B())
print(new C())
print(new D())
;(function A () {
var a = 'apple'
, b = 'banana'
, c = 'hamburger'
console.log(arguments.callee.name, a, b, c)
;(function B () {
var a = 'pine-apple'
console.log(arguments.callee.name, a, b, c)
;(function C () {
var b = 'saussage'
console.log(arguments.callee.name, a, b, c)
})()
})()
;(function D () {
var c = 'bacon'
console.log(arguments.callee.name, a, b, c)
})()
})()
@refset
Copy link

refset commented Mar 18, 2013

Wonderfully fortuitous!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment