Self-calling function in JS (one-time function)

Source: Internet
Author: User

1. Self-invocation of function---self-calling function
function's self-invocation//one-time functions (function () {Console.log ("one-time");}) ();(function (Win) {var num=20;win.num=num;}) (window);//The local variable to the parent class can be console.log (num);
2. Generate a random number object from the self-calling function, and call the random Number object method to generate a random number outside the self-calling function
Generates a Random object (function (window) {//generates a random function () {random.prototype.getrandom=function (Min,max) { Return Math.floor (Math.random () * (max-min) +min);};/ /expose the Random object to the top-level object Window}window. Random=random;}) (window); var rm=new Random (); Console.log (Rm.getrandom (0,5));  
3. Case a random small square
<script>//Generate random Number object (function (window) {function Random () {} random.prototype.getrandom=function (min,m    AX) {return Math.floor (Math.random () * (max-min) +min);    }; Exposing a local object to a window's top-level object becomes the Global object window.  Random=new Random ();    }) (window);//The semicolon must add//produce a small square object (function (window) {//console.log (Random.getrandom (0,5)), from the way the constructor is called.    Selector for the way to get the element object var map=document.queryselector (". Map"); Food's constructor function foods (width,height,color) {this.width=width| | 20;//The default small square width this.height=height| | 20;//default Small square height//horizontal axis, ordinate this.x=0;//horizontal axis randomly generated this.y=0;//ordinate randomly generated this.color=color;//small square background color this.      Element=document.createelement ("div");//small square element}//Initialize the display effect and position of the small square---display map food.prototype.init=function (map) {      Set the style of the small square var div=this.element;      div.style.position= "Absolute";//out-of-document flow div.style.width=this.width+ "px";      div.style.height=this.height+ "px";      Div.style.backgroundcolor=this.color; Add small squares to mapMap in Map.appendchild (DIV);    This.render (map);    }; Generate random position food.prototype.render=function (map) {//randomly generate horizontal ordinate var x=random.getrandom (0,map.offsetwidth/this.widt      h) *this.width;      var y=random.getrandom (0,map.offsetheight/this.height) *this.height;      This.x=x;      This.y=y;      var div=this.element;      div.style.left=this.x+ "px";    div.style.top=this.y+ "px";    };    Instanced object var fd=new food (20,20, "green");    Fd.init (map);      Console.log (fd.x+ "= = =" +fd.y); }) (window);</script>

  

Self-calling function in JS (one-time function)

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.