JavaScript closures, recursion, deep copy

Source: Internet
Author: User
Tags shallow copy

Closed Package

Understanding:The A function executes and return the b function and the b function can access a The data for the function

Benefit: The child function is stored inside the complex function, and the child function is not automatically destroyed after execution

Cons: Large memory footprint

Ex

function Bibao () {     var i=10;      return function () {         console.log (i);          return i++;}    } var bibao_task=Bibao (); Bibao_task (); Bibao_task (); Bibao_task ();

Recursive

Understanding: Child elements have child elements, child elements have child elements .....

Deep copy and shallow copy

Understanding: The difference between human cloning and shadow, memory address

Deep Copy:

Json Functions

Target=json.parse (json.stringify (obj))

$.extend of Jq

$.extend (true, target,obj)//parameter is true for deep copy, otherwise shallow copy

Recursive deep copy

varChina ={Nation:China, birthplaces:{name:' Chen '      }}//For deep replication, recursion is required to achievevarNewc={};functionCopy (CHINA,NEWC) { for(IinchChina ) {        if(typeofchina[i]=== ' object ') {Newc[i]=China[i]; Copy (China[i],newc[i])}Else{Newc[i]=China[i]}}} Copy (CHINA,NEWC); Console.log (NEWC)

JavaScript closures, recursive, deep copy

Related Article

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.