Ste*_*hen 44 javascript refactoring return-value
我正在重构一个我从开源项目中获取的大型javascript文档.许多函数使用不一致的return语句.这是我的意思的一个简单例子:
var func = function(param) {
if (!param) {
return;
}
// do stuff
return true;
}
Run Code Online (Sandbox Code Playgroud)
有时函数返回布尔值,有时返回字符串或其他东西.通常它们与return;条件内的简单语句不一致地配对.
The problem is that the code is complex. It is a parser that uses a multitude of unique RegEx matches, creates and destroys DOM nodes on the fly, etc. Preliminary testing shows that, in the above example, I could change the return; statement to become return false;, but I'm concerned that I may not realize that it had a negative impact (i.e. some feature stopped working) on the script until much later.
So my questions: Is there a benefit to using a blank return statement? Could this have been intentionally coded this way or was it just lazy? Can I change them all to return false;, or return null; or do I need to dig through every call and find out what they are doing with the results of those functions?
Guf*_*ffa 50
使用return不带值将返回值undefined.
如果该值作为评估布尔值,undefined将工作false,但如果例如值进行比较false,你会得到一个不同的行为:
var x; // x is undefined
alert(x); // shows "undefined"
alert(!x); // shows "true"
alert(x==false); // shows "false"
Run Code Online (Sandbox Code Playgroud)
因此,虽然代码应该在逻辑上返回,true或者false不是true或者undefined,您不能return;在return false;不检查返回值的使用方式的情况下更改为.
小智 20
"空白返回"语句可用于将控制转移回调用函数(或由于某种原因停止执行函数 - 例如:验证等).在大多数情况下,我使用空白return语句是在进行某种验证时.但是,我指出了为什么停止执行函数的一些指标.例如,使用错误消息在DIV元素上设置"innerText"属性.
在上面的代码中,它看起来像是一个验证.如果一切顺利,该函数返回"true".看起来调用函数解析返回值,如果它是"true",则执行语句的下一步(在调用函数中).
在上面的示例中,返回"false"而不是空白返回是一个好习惯.通过这种方式,您可以使其全部统一,并使其他程序员的生活变得轻松.
你可以解决这些不一致的问题; 但是,请确保彻底测试所有更改.测试对代码所做的每个更改都是一种很好的做法,无论它有多么小.
可能会丢失的东西(不是直接用你的例子)是你可以有一个三态对象:
var myfunc = function(testparam) {
if (typeof testparam === 'undefined') return;
if (testparam) {
return true;
}
else {
return false;
}
};
var thefirst = myfunc(true)
var thesecond = myfunc(false);
var thelast = myfunc();
alert("type:" + typeof thefirst+" value:"+thefirst);
alert("type:" + typeof thesecond+" value:"+thesecond);
alert("type:" + typeof thelast+" value:"+thelast);
Run Code Online (Sandbox Code Playgroud)
这些回报:
> type:boolean:true
> type:boolean:false
> type:undefined:undefined
Run Code Online (Sandbox Code Playgroud)
注意:在此示例中,null将返回false myfunc(null);
return;和之间完全没有区别return undefined;。调用这两个函数的结果是接收值undefined。
(以终止于代码末尾与仅从代码末尾脱落的函数体之间存在非常小的规范级别差异return,但在代码中无法检测到任何东西。¹ 调用执行从代码末尾脱落的函数也会产生值undefined。)
"use strict";
// Implicit return of `undefined`
function x() {
return;
}
// Explicit return of `undefined`
function y() {
return undefined;
}
// Execution falls off the end
function z() {
}
console.log(typeof x() === "undefined"); // true
console.log(typeof y() === "undefined"); // true
console.log(typeof z() === "undefined"); // trueRun Code Online (Sandbox Code Playgroud)
除非,当然,有什么东西已经遮蔽了undefined。遗憾的是,这仍然是可能的(尽管不是,很高兴,在全球范围内)。在那个非常前卫的边缘情况下,有一个区别:
"use strict";
(function() {
const undefined = 42;
// ^^^^^^^^^^^^^^^---- shadowing `undefined`
// Implicit return of `undefined`
function x() {
return;
}
// Explicit return of `undefined`
function y() {
return undefined;
}
// Execution falls off the end
function z() {
}
console.log(typeof x() === "undefined"); // true, `x` returns the canonical `undefined`
console.log(typeof y() === "undefined"); // false, `y` returns 42
console.log(typeof z() === "undefined"); // true, `z` (effectively) returns the canonical `undefined`
})();Run Code Online (Sandbox Code Playgroud)
¹ Usingreturn是一个突然完成,[[Call]] 转换为正常完成 w/value。从代码的末尾脱落是一个正常的完成(spec)([[Call]] 确保undefined值的供应)。但同样,这是规范级别的差异,而不是在代码中可以观察到的东西。
更改函数实际上会改变代码,因为 return;它会return false;输出不同的数据类型.
var test = function (x) {
if (!x) {
return;
}
else {
return false;
}
};
var a = test(true), b = test(false);
console.log(typeof b); // boolean
console.log(typeof a); // undefined
Run Code Online (Sandbox Code Playgroud)