Skip to content

Instantly share code, notes, and snippets.

@mde
Created October 1, 2014 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mde/82a934b6a3fa1da604be to your computer and use it in GitHub Desktop.
Save mde/82a934b6a3fa1da604be to your computer and use it in GitHub Desktop.
JavaScript noodling
> var foo = function (a) { return function (b) { return a * b; }; };
undefined
> var bar = foo(2);
undefined
> bar(5);
10
> var foo = function () { console.log(arguments.length); };
undefined
> foo();
0
undefined
> foo(true);
1
undefined
> foo(true, 1);
2
undefined
> var foo = function () { console.log(arguments[1]); };
undefined
> foo(true, 1);
1
undefined
> [1, 2, 3].slice(1, 2);
[ 2 ]
> [1, 2, 3, 4, 5].slice(1, 2);
[ 2 ]
> [1, 2, 3, 4, 5].slice(1, 4);
[ 2, 3, 4 ]
> [1, 2, 3, 4, 5].slice();
[ 1, 2, 3, 4, 5 ]
> var foo = { a: function () { console.log(this.b); };
...
> var foo = { a: function () { console.log(this.b); } };
undefined
> foo;
{ a: [Function] }
> foo.();
...
> foo.a();
undefined
undefined
> var bar = { b: true };
undefined
> foo.a.apply(bar);
true
undefined
> var foo = { a: function (c, d, e) { console.log(this.b); console.(d); } };
...
> var foo = { a: function (c, d, e) { console.log(this.b); console.log(d); } };
undefined
> foo.a.apply(bar, [1, 2, 3]);
true
2
undefined
> foo.a.call(bar, 1, 2, 3);
true
2
undefined
> var foo = function () { };
undefined
> [1, 2, 3].slice();
[ 1, 2, 3 ]
> var foo = function () { var args = Array.prototype.slice.apply(arguments); };
undefined
> var foo = function () { var args = Array.prototype.slice.apply(arguments); var arg = args.pop(); };
undefined
> var foo = function () { var args = Array.prototype.slice.apply(arguments); var arg = args.pop(); console.log(arg); };
undefined
> foo(a, b, c);
ReferenceError: a is not defined
at repl:1:5
at REPLServer.self.eval (repl.js:110:21)
at repl.js:249:20
at REPLServer.self.eval (repl.js:122:7)
at Interface.<anonymous> (repl.js:239:12)
at Interface.emit (events.js:95:17)
at Interface._onLine (readline.js:202:10)
at Interface._line (readline.js:531:8)
at Interface._ttyWrite (readline.js:760:14)
at ReadStream.onkeypress (readline.js:99:10)
> foo(true, false 2112);
...
> foo(true, false, 2112);
2112
undefined
>
undefined
>
undefined
>
undefined
> var foo = function () { var args = Array.prototype.slice.apply(arguments); return function (a) { args.push(a); console.log(args) }; };
undefined
> foo();
[Function]
> foo(1, 2, 3);
[Function]
> var bar = foo(1, 2, 3);
undefined
> bar(4);
[ 1, 2, 3, 4 ]
undefined
> var bar = foo(1, 2, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment