## 2017年12月25日 星期一

### [ JavaScript Gossip ] JavaScript 核心 : 一切都與函式有關 (Function 實例)

Source From Here

> function max(n1, n2) { return n1 > n2 ? n1: n2; }
> var maximum = max;
> max(10, 20);
20
> maximum(10, 20);
20

> var maximum = function max(n1, n2){ return n1 > n2 ? n1:n2; }
> var maximum = max;
> max(1, 2);
2
> maximum(1, 2);
2

1. var number = 10;
2. var obj = { x : 10 };
3. var array = [123];
4. var func = function() {
5.     // do something...
6. };
Function 實例 介紹 :

> var max = new Function('num1', 'num2', 'return num1 > num2 ? num1 : num2');
> max(5, 6);
6

> var printIt = function(element){ console.log(element); }
> [1, 2, 3].forEach(printIt);
1
2
3

> var comparator = function(n1, n2) { return n1 - n2; }
> [5, 1, 7, 3, 2].sort(comparator);
1,2,3,5,7

> [1, 2, 3].forEach(function(e){ console.log(e); })
1
2
3

> [5, 1, 7, 3, 2].sort(function(n1, n2){ return n1 - n2; })
1,2,3,5,7

> (function() {
> print('anonymous function...');
> })();

anonymous function...

> var maximum = function max(num1, num2) {
> return num1 > num2 ? num1 : num2;
> };

> maximum(10, 20);
20
> max(10, 20);
js: "", line 6: uncaught JavaScript runtime exception: ReferenceError: "max" is not defined.
at :6

> var gcd = function g(n1, n2){ return n2!=0? g(n2, n1%n2):n1; } // 計算最大公因數
> gcd(10, 5);
5

> var gcd = function(num1, num2) {
> return num2 != 0 ? arguments.callee(num2, num1 % num2) : num1;
> };

> gcd(20, 10);
10

> var gcd = function (n1, n2){ return n2!=0? arguments.callee(n2, n1%n2):n1; }
> gcd.length;
2

1. func();
2. function func() {
3.     print('func');
4. }

your_workspace> java org.mozilla.javascript.tools.shell.Main f.js
func

# node f.js
func

1. func();
2. var func = function() {
3.     print('func');
4. };

your_workspace> java org.mozilla.javascript.tools.shell.Main f.js
js: uncaught JavaScript runtime exception: TypeError: func is not a function, it is undefined.

> function func() { print('func'); }
> func();
func
> this.func();
func

### [ Py DS ] Ch1 - IPython: Beyond Normal Python

Source From  Here   Keyboard Shortcuts in the IPython Shell   If you spend any amount of time on the computer, you’ve probably found a u...