JavaScript variable Assignment

Source: Internet
Author: User
Tags shallow copy

In JS often encounter the assignment of a variable to a new variable, which is easy to implement for the basic type, directly by the equal sign assignment is possible, for reference types can not be the Case. (note: like functions, Regular can also be assigned directly by the equal Sign)

Here I wrote a copy of the value of the function, can be deep copy, also can be shallow copy, for deep copy only need to set the second parameter to True

functionclone (data,deep) {varClonedata =undefined; vardata = Arguments[0],deep = Arguments[1]; if(!deep ) {        returndata; }Else{        if(typeofDeep = = ' Boolean '){            if(className (data) = = = ' Object ') {clonedata= {};  for(keyinchData) {                    if(data.hasownproperty (key)) {clonedata[key]=Arguments.callee (data[key],deep); }                }                returnclonedata; }Else if(className (data) = = = ' Array ') {clonedata= [];  for(vari = 0,len = data.length;i<len;i++) {clonedata[i]=Arguments.callee (data[i],deep); }                returnclonedata; }Else{                returndata}}Else{            returndata; }    }}functionclassName (obj) {returnObject.prototype.toString.call (obj). Slice (8,-1);}

JavaScript variable Assignment

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.