有时,优雅的实现是一个函数。不是方法。不是类。不是框架。只是函数。
- John Carmack,游戏《毁灭战士》首席程序员
var
obj1 = {value: 1},
obj2 = {value: 2},
obj3 = {value: 3};
var values = [];
function accumulate(obj) {
values.push(obj.value);
}
accumulate(obj1);
accumulate(obj2);
console.log(values); // Output: [obj1.value, obj2.value]
function accumulate2(obj) {
var values = [];
values.push(obj.value);
return values;
}
console.log(accumulate2(obj1)); // Returns: [obj1.value]
console.log(accumulate2(obj2)); // Returns: [obj2.value]
console.log(accumulate2(obj3)); // Returns: [obj3.value]
var ValueAccumulator = function(obj) {
var values = []
var accumulate = function() {
values.push(obj.value);
};
accumulate();
return values;
};
var ValueAccumulator = function() {
var values = [];
var accumulate = function(obj) {
if (obj) {
values.push(obj.value);
return values;
} else {
return values;
}
};
return accumulate;
};
//This allows us to do this:
var accumulator = ValueAccumulator();
accumulator(obj1);
accumulator(obj2);
console.log(accumulator());
// Output: [obj1.value, obj2.value]
ValueAccumulator = ->
values = []
(obj) ->
values.push obj.value if obj
values
// 把信息打印到屏幕中央的函数
var printCenter = function(str) {
var elem = document.createElement("div");
elem.textContent = str;
elem.style.position = 'absolute';
elem.style.top = window.innerHeight / 2 + "px";
elem.style.left = window.innerWidth / 2 + "px";
document.body.appendChild(elem);
};
printCenter('hello world');
// 纯函数完成相同的事情
var printSomewhere = function(str, height, width) {
var elem = document.createElement("div");
elem.textContent = str;
elem.style.position = 'absolute';
elem.style.top = height;
elem.style.left = width;
return elem;
};
document.body.appendChild(
printSomewhere('hello world',
window.innerHeight / 2) + 10 + "px",
window.innerWidth / 2) + 10 + "px"));
var messages = ['Hi', 'Hello', 'Sup', 'Hey', 'Hola'];
messages.map(function(s, i) {
return printSomewhere(s, 100 * i * 10, 100 * i * 10);
}).forEach(function(element) {
document.body.appendChild(element);
});
// 写匿名函数的标准方式
function() {
return "hello world"
};
// 匿名函数可以赋值给变量
var anon = function(x, y) {
return x + y
};
// 匿名函数用于代替具名回调函数,这是匿名函数的一个更常见的用处
setInterval(function() {
console.log(new Date().getTime())
}, 1000);
// Output: 1413249010672, 1413249010673, 1413249010674, ...
// 如果没有把它包含在一个匿名函数中,他将立刻被执行,
// 并且返回一个undefined作为回调函数:
setInterval(console.log(new Date().getTime()), 1000)
// Output: 1413249010671
function powersOf(x) {
return function(y) {
// this is an anonymous function!
return Math.pow(x, y);
};
}
powerOfTwo = powersOf(2);
console.log(powerOfTwo(1)); // 2
console.log(powerOfTwo(2)); // 4
console.log(powerOfTwo(3)); // 8
powerOfThree = powersOf(3);
console.log(powerOfThree(3)); // 9
console.log(powerOfThree(10)); // 59049
var
obj1 = { value: 1 },
obj2 = { value: 2 },
obj3 = { value: 3 };
var values = (function() {
// 匿名函数
var values = [];
return function(obj) {
// 有一个匿名函数!
if (obj) {
values.push(obj.value);
return values;
} else {
return values;
}
}
})(); // 让它自执行
console.log(values(obj1)); // Returns: [obj.value]
console.log(values(obj2)); // Returns: [obj.value, obj2.value]
obj1 = { value: 1 }
obj2 = { value: 2 }
obj3 = { value: 3 }
values = do ->
valueList = []
(obj) ->
valueList.push obj.value if obj
valueList
console.log(values(obj1)); # Returns: [obj.value]
console.log(values(obj2)); # Returns: [obj.value, obj2.value]
// 每个函数占用一行来调用,不如…… arr = [1, 2, 3, 4]; arr1 = arr.reverse(); arr2 = arr1.concat([5, 6]); arr3 = arr2.map(Math.sqrt); // ……把它们串到一起放在一行里面 console.log([1, 2, 3, 4].reverse().concat([5, 6]).map(Math.sqrt)); // 括号也许可以说明是怎么回事 console.log(((([1, 2, 3, 4]).reverse()).concat([5, 6])).map(Math.sqrt));
var foo = function(n) {
if (n < 0) {
// 基准情形
return 'hello';
} else {
// 递归情形
return foo(n - 1);
}
}
console.log(foo(5));
var getLeafs = function(node) {
if (node.childNodes.length == 0) {
// base case
return node.innerText;
} else {
// recursive case:
return node.childNodes.map(getLeafs);
}
}
function gcd(a, b) {
if (b == 0) {
// 基准情形 (治)
return a;
} else {
// 递归情形 (分)
return gcd(b, a % b);
}
}
console.log(gcd(12,8));
console.log(gcd(100,20));
gcb = (a, b) -> if b is 0 then a else gcb(b, a % b)
var mergeSort = function(arr) {
if (arr.length < 2) {
// 基准情形: 只有0或1个元素的数组是不用排序的
return items;
} else {
// 递归情形: 把数组拆分、排序、合并
var middle = Math.floor(arr.length / 2);
// 分
var left = mergeSort(arr.slice(0, middle));
var right = mergeSort(arr.slice(middle));
// 治
// merge是一个辅助函数,返回一个新数组,它将两个数组合并到一起
return merge(left, right);
}
}
// 理想化的JavaScript伪代码: var infinateNums = range(1 to infinity); var tenPrimes = infinateNums.getPrimeNumbers().first(10);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有