function Stack() { this.dataStore = []; this.top = 0;//top的值等同于数组内的元素个数 this.push = push; this.pop = pop; } function push(element) { this.dataStore[this.top++] = element; } function pop() { return this.dataStore[--this.top]; } function fact(n) { var s = new Stack(); while (n > 1) { s.push(n--); } var product = 1; while (s.top > 0) { product *= s.pop(); } return product; } alert(fact(5)); // 显示 120
原文:http://www.cnblogs.com/feile/p/5372703.html