[英]Nodejs module.exports and Shorthand if/else
What does the line 'undefined' != typeof Customer ? Customer : module.exports
'undefined' != typeof Customer ? Customer : module.exports
行是什么'undefined' != typeof Customer ? Customer : module.exports
'undefined' != typeof Customer ? Customer : module.exports
in the following code snippet do? 'undefined' != typeof Customer ? Customer : module.exports
中的以下代码片段是做什么的? And why do you wrap everything within (function(){})
? 为什么将所有内容都包装在
(function(){})
? I cant seem to decipher its meaning 我似乎无法解读其含义
This snippet comes from a library file. 此摘录来自库文件。
(function (Customer) {
Customer.Base = {
//...
}
})(
'undefined' != typeof Customer ? Customer : module.exports
);
This is ternary that is used to determine what is passed to the function. 这是用于确定传递给函数的三元数。 The function is an Immediately-invoked function exppression (IIFE) - a function that is created and immediately invoked.
该函数是立即调用的函数表达式(IIFE)-创建并立即调用的函数。
//the condition
'undefined' != typeof Customer
//true value
? Customer
//false value
: module.exports
If Customer
is undefined, this is the same result: 如果未定义
Customer
,则结果相同:
(function (Customer) {
Customer.Base = {
//...
}
})(module.exports);
So this code block is creating and immediately invoking a function that does something to a Customer
. 因此,此代码块正在创建并立即调用对
Customer
有作用的函数。 If Customer
is defined, Customer
is passed to the function as the function argument which is also named Customer
. 如果定义了
Customer
,则将Customer
作为函数参数传递给函数,该函数参数也称为Customer
。 If Customer
is undefined, module.exports
is passed to the function as the Customer
argument. 如果未定义
Customer
, module.exports
作为Customer
参数传递给函数。 This code could be rewritten as: 此代码可以重写为:
var param;
if ('undefined' != typeof Customer) {
param = Customer;
} else {
param = module.exports;
}
function myFunc(Customer) {
Customer.Base = {
//...
}
}
myFunc(param);
It may be easier to understand in a more generic example. 在更通用的示例中可能更容易理解。
Here's an IIFE, a function that is created and immediately invoked: Live demo (click). 这是IIFE,即创建并立即调用的函数: 实时演示(单击)。
(function(param) {
console.log(param);
})('some param!');
and here's that same function, using ternary to determine the param value: Live demo (click). 这是使用三元数确定参数值的相同函数: Live demo(单击)。
var x = true;
//var x = false;
var value = x ? 'true value!' : 'false value!';
(function(param) {
console.log(param);
})(value);
Change x
(the condition) to true or false and see that the assigned value of value
is changed accordingly. 将
x
(条件)更改为true或false,并观察到value的value
已相应更改。
You may often see the ternary condition wrapped in ()
, but they are not necessary: 您可能经常会看到三元条件包装在
()
,但它们不是必需的:
('undefined' != typeof Customer) ? Customer : module.exports
Further, it is more typical to see that statement asked the opposite way: 此外,更典型的是看到该语句提出相反的要求:
typeof Customer === 'undefined'
and it is likely that a simple loose equality check for "truthiness" would suffice here: 并且简单的对“真实性”的松散相等检查可能在这里就足够了:
Customer ? Customer : module.exports
and that could again be simplified to: 并且可以再次简化为:
Customer || module.exports
//if "Customer" is truthy, it will be used, otherwise, module.exports is used
Also note that with an IIFE, the can be })()
or {())
, the latter being the more typical syntax. 还要注意,对于IIFE,可以是
})()
或{())
,后者是更典型的语法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.