问题描述
我看过幻灯片,提供了,一个node.js框架。
这是JavaScript吗?
有人可以解释该代码中发生了什么吗?
I全都输了。
是纯JavaScript,它是函数链接模式。
第一行(fab = require(fab))
包括 fab
function并返回对它的引用。
所有后续括号都是函数调用,每个函数调用可能会一次又一次地返回相同的函数。 / p>
该模式可能看起来像这个简化的例子:
var foo = function(arg){
//检测参数是什么
if(typeof arg =='function'){
//用arg
做某事console.log('function:'+ arg());
} else if(arg instanceof RegExp){
// arg是一个RegExp ...
console.log('一个RegExp:'+ arg);
} else if(typeof arg ==string){
// arg是一个字符串
console.log('A string:'+ arg);
}
返回foo; //返回对自身的引用
};
(foo)
(function(){returnFoo;})
(/ bar /)
(baz!);
输出:
函数:Foo
A RegExp:/ bar /
字符串:baz!
I've seen a slide that presented Fab, a node.js framework.
Is this JavaScript?
Could someone explain what is going on in that code?
I'm all lost.
Is plain JavaScript, it is a function chaining pattern.
The first line, ( fab = require("fab") )
includes the fab
function and returns a reference to it.
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!
这篇关于Node.js上的Javascript FAB框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!