美文网首页
js 的自执行

js 的自执行

作者: 龙权 | 来源:发表于2016-03-25 21:21 被阅读20次
// Either of the following two patterns can be used to immediately invoke
  // a function expression, utilizing the function's execution context to
  // create "privacy."

  (function(){ /* code */ }()); // Crockford recommends this one
  (function(){ /* code */ })(); // But this one works just as well

  // Because the point of the parens or coercing operators is to disambiguate
  // between function expressions and function declarations, they can be
  // omitted when the parser already expects an expression (but please see the
  // "important note" below).

  var i = function(){ return 10; }();
  true && function(){ /* code */ }();
  0, function(){ /* code */ }();

  // If you don't care about the return value, or the possibility of making
  // your code slightly harder to read, you can save a byte by just prefixing
  // the function with a unary operator.

  !function(){ /* code */ }();
  ~function(){ /* code */ }();
  -function(){ /* code */ }();
  +function(){ /* code */ }();

  // Here's another variation, from @kuvos - I'm not sure of the performance
  // implications, if any, of using the `new` keyword, but it works.
  // http://twitter.com/kuvos/status/18209252090847232

  new function(){ /* code */ }
  new function(){ /* code */ }() // Only need parens if passing arguments

相关文章

  • js 的自执行

  • js()()匿名自执行函数

    js匿名自执行函数的写法为()(),主要是用与编写js插件,使用自执行函数能够确保该函数能够自行执行,而不需要而外...

  • JS中的自执行函数

    自执行函数为两个括号其他一个中为匿名函数,别一个括号表示运行。 上面即使在某个对象内部,this依然代表windo...

  • JS匿名自执行函数(IIFE)

    JS自执行函数又称为IIFE,在我们开发过程中会使用到大量的自执行函数。 IIFE写法: 使用建议:在使用只执行函...

  • js引擎的执行机制

    js引擎的执行机制 JS的Event Loop是JS的执行机制,理解JS的执行,必须理解Event Loop JS...

  • js立即执行函数

    js中(function(){...})()立即执行函数写法理解 转自segmentfault chichttps...

  • 简单了解JS自执行函数

    1、常见的function写法如下: 2、自执行函数 自执行函数也叫立即调用函数。在函数体后面加括号就能立即调用,...

  • JS自执行函数,匿名函数

    自执行函数 先来看个最简单的自执行函数 相当于声明并调用 自执行函数也可以有名字 自执行函数也可以传参 下面我们来...

  • Auto.JS简介与教程

    什么是Auto.JS? Auto.JS是Android平台上的JavaScript自动化工具。 它的本质是可执行自...

  • js的加载顺序

    先加载执行a.js,完毕后再加载执行b.js。 先执行a.js,完毕后再执行b.js。 这就不可以确定谁先执行了,...

网友评论

      本文标题:js 的自执行

      本文链接:https://www.haomeiwen.com/subject/kjlhlttx.html