javascript作用_防止JavaScript产生副作用

javascript作用

JavaScript is very dynamic these days but I still see a lot of legacy code, whether it be for optimal backward compatibility or simply that the code hasn't been maintained.  One of the practices that makes me cringe is coding that creates unwanted side effects.  What's a side effect?  A piece of code whereby a variable is created and available throughout a scope when it doesn't need to be.  Let me show you a few examples and how to avoid these unwanted side effects.

如今,JavaScript是非常动态的,但我仍然看到很多旧代码,无论是为了实现最佳的向后兼容性,还是仅仅是未维护代码。 使我畏缩的一种做法是编码,该编码会产生有害的副作用。 有什么副作用? 一段代码,通过该代码可以创建变量,并在不需要时将其在整个作用域中使用。 让我向您展示一些示例,以及如何避免这些不良副作用。

Array.prototype.forEach()代替for(var x = ...) (Array.prototype.forEach() instead of for(var x = ...))

Looping through a JavaScript array was traditionally done via a for() loop:

传统上,通过for()循环在JavaScript数组中循环:


var myArray = [1, 2, 3];

for(var x=0, length = myArray.length; x < length; x++) {
	// ...
}

// "x" and "length" are side effects


The side effect of this pattern is at minimum the running index, if not the length as well -- they are available within the entire scope.  Array prototype methods like map, forEach, and every allow the developer to avoid these side effects:

这种模式的副作用至少是运行索引,即使不是长度也是如此-它们在整个范围内都可用。 Array原型方法,例如mapforEachevery方法,使开发人员可以避免以下副作用:


[1, 2, 3].forEach(function(item, index, array) {
	// No side effects! :)
});


No "utility" variables need to be created for the looping, thus avoiding side effects. This is called "functional" programming.

无需为循环创建“实用程序”变量,从而避免了副作用。 这称为“功能”编程。

自执行功能 (Self-Executing Functions)

If you haven't read Hiding Your Privates with JavaScript, and you don't know how to keep private variables in JavaScript, take a few minutes to read it.  The same pattern provided in that post allows you to avoid side effects via self-executing functions:

如果您还没有阅读使用JavaScript隐藏私人信息 ,并且不知道如何在JavaScript中保留私人变量,请花几分钟的时间阅读它。 该帖子中提供的相同模式允许您通过自执行函数避免副作用:


// Example from MooTools source...

Browser.Request = (function(){

	var XMLHTTP = function(){
		return new XMLHttpRequest();
	};

	var MSXML2 = function(){
		return new ActiveXObject('MSXML2.XMLHTTP');
	};

	var MSXML = function(){
		return new ActiveXObject('Microsoft.XMLHTTP');
	};

	return Function.attempt(function(){
		XMLHTTP();
		return XMLHTTP;
	}, function(){
		MSXML2();
		return MSXML2;
	}, function(){
		MSXML();
		return MSXML;
	});

})();

// The three vars are stuck in the self-executing function, they don't "leak" out


The gist is that you can do loads of processing within the self-executing function (a new scope) without allowing variables leaking out -- the only item returned or leaked is the desired return value.

要点是,您可以在自执行函数(新作用域)中执行大量处理,而不会让变量泄漏出去-返回或泄漏的唯一项是所需的返回值。

Tightening up your code includes avoiding side effects and JavaScript makes it easy if you follow these basic practices!

加强代码包括避免副作用,如果您遵循这些基本做法,JavaScript会很容易!

翻译自: https://davidwalsh.name/preventing-sideeffects-javascript

javascript作用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值