Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View modules.js
1
node_modules/A/node_modules/B/node_modules/C/all.js
View modules.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
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())
 
View modules.js

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.

View modules.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 
;(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)
})()
 
 
})()

Wonderfully fortuitous!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.