[英]Javascript FAB framework on Node.js
Is plain JavaScript, it is a function chaining pattern. 是纯JavaScript,它是一个函数链模式。
The first line, ( fab = require("fab") )
includes the fab
function and returns a reference to it. 第一行
( fab = require("fab") )
包括fab
函数并返回对它的引用。
All the subsequent parentheses are function calls, each function invocation returns probably the same function again and again. 所有后续括号都是函数调用,每个函数调用可能会一次又一次地返回相同的函数。
The pattern probably looks like this simplified example: 该模式可能看起来像这个简化的例子:
var foo = function (arg) {
// detect what the argument is
if (typeof arg == 'function') {
// do something with arg
console.log('function: '+arg());
} else if (arg instanceof RegExp) {
// arg is a RegExp...
console.log('A RegExp: '+arg);
} else if (typeof arg == "string") {
// arg is a string
console.log('A string: '+arg);
}
return foo; // return a reference to itself
};
(foo)
(function() { return "Foo "; })
(/bar/)
(" baz!");
Outputs: 输出:
function: Foo A RegExp: /bar/ A string: baz!
That's hard to follow indeed; 这确实难以理解; it doesn't really look like Javascript at all...
它根本不像Javascript ......
Anyway, FAB takes advantage of returning a pointer to the function which was called. 无论如何,FAB利用返回指向被调用函数的指针。 For example:
例如:
function doSomething(str){
alert(str);
return arguments.callee;
}
// Alerts 'hi' and then 'there'
doSomething('hi')('there');
Of course you can implement extra conditions, like counting the number of arguments or checking the type of arguments passed in. For example: 当然,您可以实现额外的条件,例如计算参数的数量或检查传入的参数的类型。例如:
function doSomething(){
if(arguments.length == 1){
alert(arguments[0])
}
else if(arguments.length == 2){
alert(arguments[0] + arguments[1]);
}
return arguments.callee;
}
doSomething
("Hi, 3 + 4 is:")
(3, 4);
The last example alerts: 最后一个示例提醒:
> Hi, 3 + 4 is:
> 7
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.