js--closed functions, closures, built-in objects

Source: Internet
Author: User

Closed function: Another way of writing anonymous functions in JavaScript, creating a function that executes without naming it at the beginning

Example:

1)

(function() {    var str = ' Welcome to my home page ';    alert (str);    A+ +;}) ();

2)

! function () {    var str = ' Welcome to my homepage ';    alert (str);    A+ +;} ();

3)

~function() {    var str = ' Welcome to my homepage ';    alert (str);    A+ +;} ();

Note: Add in front of the closed function; You can avoid some unnecessary problems ~

Closures: function nesting functions, intrinsic functions can refer to parameters and variables of external functions, parameters and variables are not reclaimed by garbage collection mechanism.

Closure function:

1) A variable is stationed in memory for a long time and can be used to cycle the value of the index.

        var aLi = document.getelementsbytagname (' li ');                  for (var i=0;i<ali.length;i++)        {            (function  (i) {                     function  () {                            alert (i);                }             }) (i);        }    

2) Private variable counter, external inaccessible, avoid the pollution of global variables.

        var count = aa ();         function AA () {            var a = 1;             function BB ()            {                a++                ; return A;            }             return BB;        }        Alert (count ());        Alert (count ());

Built-in objects:

1) Document

Document.referrer//Get the address of the previous Jump page (requires server environment)

2) Location

Window.location.href//Get or reset the URL address;

Window.location.search//Get address Parameters section;

Window.location.hash//Get the page anchor point or hash value;

 <!    DOCTYPE html> window.onload  = function   () { var  Dat = Window.location.search;  var  ospan = document.getElementById (' Span01 '             var  arr = dat.split (' = '  var  a = Arr[1];        ospan.innerhtml  = A;  </script>

3) Math

Math.random//Get a random number of 0-1;

Math.floor//downward rounding;

Math.ceil//upward rounding;

js--closed functions, closures, built-in objects

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.