Home > Web Front-end > JS Tutorial > Usage and advantages of (function(){})()_javascript skills

Usage and advantages of (function(){})()_javascript skills

WBOY
Release: 2016-05-16 19:16:59
Original
1286 people have browsed it

In Google's ad js, I found a statement similar to this
//I used alert("test") to replace the statement inside

(function(){alert("test")}) ()
Tested it, similar to directly executing

alert("test")
So what are the benefits of this usage? I hope everyone can talk about it!

function xx(){...}
Equivalent to
var xx;
xx=function(){...}

function( ){....}Return a Function object
(Function object)() is to call the function

The intuitive benefit is to organize several statements that need to be executed sequentially into a unit, making the logic Clear, and there is no need to give it a name
It may be due to economical considerations: the anonymous function is like a temporary variable and can be destroyed immediately after execution, of course together with the local variables in it

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template