Introduction to the use of anonymous functions for JS

Source: Internet
Author: User
Tags anonymous object object

  The function of anonymous functions is to create a closed area, outside can not access the variables and methods inside, here is a description of what is anonymous function and how to use

1. Overview of anonymous functions     first recognition of anonymous functions or in the jquery source, open jquery first see the   code as follows: (function (window, undefined) { .......................}) (window);    This is an anonymous function, red as the parameter, the function of the anonymous function is to create a closed area, outside can not access the variables and methods inside.     Since you can't access it, how can you invoke jquery? This is because the anonymous function of JQuery has two words (the blue Word):  code as follows: (function (window, undefined) {   //Define a local copy of Jquery&nbs P var jQuery = function (selector, context) { //The JQuery object is actually just the Init constructor ' enhanced ' &N Bsp Return to New JQuery.fn.init (selector, context); },    .........    window.jquery = window.$ = JQ uery;   }) (window);    passed the jquery to window in the anonymous function, which is why the pass of the parameter passes the window. So every call to jquery in the future is actually called the JQuery object for window.     Call yourself inside the method by jquery. The outside is not callable, which guarantees security and does not conflict.     2. Then the above topic, about jquery plug-ins     The following is a partial code for the pagination control I wrote:  code as follows:;(function ($) {    $.fn.tabing = function (ARG) {  instance = new Plugin (this, arg); };  var instance = null;  function Plugin (element) {  This._tabs = $ (Element);    This._tabli = $ ("a[href*= ' # ']", this. _tabs);  this._tabdiv = This._tabs.siblings (). Filter ("div[id*= ' tab ')";  this.init ();  Plugin.prototype = {  init:function () {  this._tabli.addclass ("unselected");  this._tabli.eq (0). AddClass ("selected");  this._tabdiv.css ("Display", "none");  This._tabdiv.eq (0). CSS ("Display", "block");   This._tabli.each (function () {  $ (this). Bind ("click", Function () {  for (var i = 0;i<instance._ tabdiv.length;i++) {  Instance._tabdiv.eq (i). CSS ("display", "none"); }  Instance._tabdiv.filter ("#" +$ (This). attr ("href"). Split (' # ') [1]). CSS ("Display", "Block"); });   } }   }) ( jquery);    Note the red word, in fact, the jquery plugin is also in the Write anonymous function, so as to ensure the independence of each plug-in, not called Plug-ins, Red Word $. Fn.tabing explains that there is a tabing in this plugin to the jquery fn,    so the jquery object outside can call tabing directly, which is the only contact between the plugin and the outside world.     3. I'm done. The use of the jquery plugin for anonymous functions, say again WINdow anonymous functions     In fact, jquery itself is the anonymous function of window, that is, 1th, how do we write the anonymous function of window?     After writing an anonymous function, there is an interface in the function that interacts with the window, such as the following:  code: (function () {  function Getobjbyid (ID) {  return document.getElementById (ID); }  function __addclass (id,classname,classvalue) {  $ (ID). style.classname=classvalue; }  window.addclass=__addclass; }) ();    is also looking at the red Word, This allows addclass () to be invoked outside the anonymous function, but cannot invoke Getobjbyid ().     4. Anonymous functions are also executed at parse time     The following:  code as follows: function video () {};  function Movie () {};    var _video = new Video ();  _video.size = 3;  _video.tostring = function () {  return "video";  P _video.getname = function () {  return "videoxxx"; };  var _movie = new Movie ();  (function (parent, Child) {  for (var ele in parent) {  if (!child[ele)///When the child does not contain the property or method, a copy of parent   Child[ele] = par ent[ele]; } }) (_video, _movie); How anonymous functions are called     aleRT (_movie.size); 3  alert (_movie.tostring ()); [Object object]  Alert (_movie.getname ()); videoxxx     
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.