什么是没有操作的JavaScript约定? [英] What is the JavaScript convention for no operation?

查看:113
本文介绍了什么是没有操作的JavaScript约定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是无操作的JavaScript约定?


  • 一个选项只是一个空函数: function ){}

  • jQuery提供 $。noop(),它简单地调用上面的空函数。



在上下文中,所有这些工作都不会在Chrome中引发错误:

  var a = 2; 
(a === 1)? alert(1):function(){};
(a === 1)? alert(1):$ .noop();
(a === 1)?警报(1):错误;
(a === 1)? alert(1):0;

编辑:许多人回应说:不要这样!改变代码结构! 这让我想起了一个帖子,其中有人问如何嗅探浏览器。他收到了大量的帖子,说:不要那么做!它是邪恶的,但没有人告诉他如何嗅探浏览器。这不是代码审查。想象一下,您正在处理遗留的代码,无法更改,并且没有传入某个函数,它会抛出一个错误。或者,简而言之,这就是客户需要的方式,而且他们正在向我付钱。 恭喜,请回答问题:在JavaScript中指定无操作功能的最佳方式是什么?



编辑2:如何关于其中之一?

  true; 
false;
0;
1;
null;


解决方案

要回答原始问题,最优雅整洁在纯Javascript中实现noop函数(正如这里所讨论的)的 Function.prototype的即可。下面的代码片段显示了它的用法:

  setTimeout(Function.prototype,10000); 

所有三种浏览器(Chrome,Firefox和IE 11)都不会导致解释器在10在执行上面的步骤时可以执行一次(可以在调试器中使用step over模式一步步测试)。

虽然这是一个真正的noop,因为大多数浏览器似乎不要执行这样定义的noop(并因此节省CPU周期),可能会有一些与此相关的性能问题(正如其他人在评论中或其他答案中所提及的那样)。


$然而,这就是说,你可以很容易地定义你自己的noop函数,事实上,许多库和框架也提供了noop函数。下面是一些例子:

  var noop = function(){}; //在ES3或ES5中定义自己的noop 
const noop =()=> {}; //或在ES6
setTimeout(noop,10000)中定义您自己的noop; //使用预定义的noop

setTimeout(function(){},10000); //直接在ES3或ES5中使用
setTimeout(()=> {},10000); //直接在ES6中作为Lambda使用(箭头函数)

setTimeout(angular.noop,10000); //使用angular 1.x
setTimeout(jQuery.noop,10000); //与jQuery一起使用

以下是各种noop函数实现(或相关讨论或谷歌搜索):



Angular 1.x Angular 2+ (似乎没有原生的
实现 - 如上所示使用您自己的), Ember jQuery Lodash NodeJS Ramda 反应 (似乎没有本地实现 - 如上所示使用您自己的实现), RxJS
下划线



BOTTOM LINE :尽管Function.prototype是在Javascript中表达一个noop的优雅方式,但是,可能会有一些与其使用相关的性能问题。因此,您可以定义和使用自己的(如上所示),或者使用您在代码中可能使用的库/框架定义的一个。


What is the JavaScript convention for no operation?

  • One option is simply an empty function: function() {}
  • jQuery offers $.noop(), which simply calls the empty function above.
  • Is it acceptable to simply enter a value of false or 0?

In context... all of these work without throwing an error in Chrome:

var a = 2;
(a === 1) ? alert(1) : function() {};
(a === 1) ? alert(1) : $.noop();
(a === 1) ? alert(1) : false;
(a === 1) ? alert(1) : 0;

EDIT: A lot of people responded with, "don't do this! Change the code structure!" This reminds me of a post where someone asked how to sniff the browser. He received a barrage of posts saying, "DON'T DO THAT! IT'S EVIL," but nobody told him how to sniff the browser. This is not a code review. Imagine that you are dealing with legacy code that can't be changed, and without some function passed in, it will toss an error. Or, simply, that's the way the customer wants it, and they're paying me. So, respectfully, please answer the question: What is the best way to specify a "no operation" function in JavaScript?

EDIT2: How about one of these?

true;
false;
0;
1;
null;

解决方案

To answer the original question, the most elegant and neat implementation of a noop function in pure Javascript (as is also discussed here) is Function.prototype. The snippet below shows it's usage:

setTimeout(Function.prototype, 10000);

All three browsers (Chrome, Firefox and IE 11) do not cause the interpreter to come back after 10 seconds when executing the above (you can test by step using "step over" mode in the debugger).

Although this is a "true noop" since most browsers seem to do nothing to execute the noop defined this way (and hence save CPU cycles), there might be some performance issues associated with this (as is also mentioned by others in comments or in other answers).

However, that being said, you can easily define your own noop function and, infact, many libraries and frameworks also provide noop functions. Below are some examples:

var noop = function () {};           // Define your own noop in ES3 or ES5
const noop = () => {};               // OR Define your own noop in ES6
setTimeout(noop, 10000);             // Using the predefined noop

setTimeout(function () {} , 10000);  // Using directly in ES3 or ES5
setTimeout(() => {} , 10000);        // Using directly in ES6 as Lambda (arrow function)

setTimeout(angular.noop, 10000);     // Using with angular 1.x
setTimeout(jQuery.noop, 10000);      // Using with jQuery

Here is an alphabetical list of various implementations of noop functions (or related discussions or google searches):

Angular 1.x, Angular 2+ (Does not seem to have a native implementation - use your own as shown above), Ember, jQuery, Lodash, NodeJS, Ramda, React (Does not seem to have a native implementation - use your own as shown above), RxJS, Underscore

BOTTOM LINE: Although Function.prototype is an elegant way of expressing a noop in Javascript, however, there might be some performance issues related to it's use. So, you can define and use your own (as shown above) or use one defined by the library/framework that you might be using in your code.

这篇关于什么是没有操作的JavaScript约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆