OA Credit disk source code to build JS Copy method Jilin Fast three source for sale

Source: Internet
Author: User
Tags hasownproperty

OA Credit disk source code to build qq1146616888 JS Copy method Jilin fast three source sale 1146616888.com

JS has a deep copy and a shallow copy of two copies of the form, the following summary of common methods, convenient for normal work review use

One, shallow copy

1. Shallow copy of JSON object

var newObj = Json.parse (json.stringify (someobj))

2, Es6 object.assign ()

Const OBJA = {name: ' CC ', age:18}
Const OBJB = {address: ' Beijing '}
Const OBJC = {}//This is the target object
Const OBJ = object.assign (OBJC, Obja, OBJB)//We will export Obja objb OBJC obj separately to see
Console.log (Obja)//{name: ' CC ', age:18}
Console.log (OBJB)//{address: ' Beijing '}
Console.log (OBJC)//{name: ' cc ', age:18, Address: ' Beijing '}
Console.log (obj)//{name: ' cc ', age:18, Address: ' Beijing '}

// 是的,目标对象ObjC的值被改变了。// so,如果objC也是你的一个源对象的话。请在objC前面填在一个目标对象{}Object.assign({}, objC, objA, objB)

3. ES6 Spread Operator expand operator syntax

var obj = {' Data ': [One, 2, 3], ' name ': ' Mfg ',
Fn:function () {}
}; var objnew = {... obj
};

4. Traverse

function Sallowcopy (source) {//source is not an object, but a case of "primitive type"
Description of the original type see HTTP://WWW.JIANSHU.COM/P/B161AEECB6D6
if (null = = Source | | "Object"! = typeof source) return source;
In other cases, source is treated as a simple object.
var target = {}; for (var key in source) {if (Source.hasownproperty (key)) {//Copy only own properties
Target[key] = Source[key];
}
} return target;
}/ This shallow copy copies all the [enumerable properties] on the source object to the target object, excluding the properties on the prototype chain. /

Second, deep copy

1, with the help of Lodash's merge method

Import merge from ' Lodash/object/merge '; function Commentsbyid (state = {}, action) {switch (action.type) {default: {if (a Ction.entities && action.entities.comments) {return merge ({}, State, action.entities.comments.byId);
} return state;
}
}
}

3, there are a large number of deep copies with immutable library

const {MAP} = require (' immutable ')
Const MAP1 = Map ({a:1, b:2, c:3})
Const MAP2 = map1.set (' B ', 50)
Map1.get (' B ')//2map2.get (' B ')//50 keep data as far as possible

4, jquery $.extend () If no first argument is a shallow copy

$.extend (True, {}, obj)

5. Methods of JSON objects

var obj2 = Json.parse (json.stringify (OBJ1))

People familiar with JS are certainly not unfamiliar with these two methods, using the two native JSON objects can be very convenient to achieve deep copy of the object.

This approach also has drawbacks:

只能复制能用json表示的属性,比如String、Number、Array等,对于不能用json表示的属性例如Function、Regexp等则会丢失对象的原型链丢失复制效率较低

Despite these shortcomings, this approach is enough to handle most of the situation.

6. Recursive replication

function Clonedeep (obj) {if (obj = = NULL | | typeof obj!== ' object ') return obj var newObj = array.isarray (obj)? []: {} for (let me in obj) {if (Obj.hasownproperty (i)) {var value = Obj[i]
Newobj[i] = typeof value = = = ' object '? Clone (value): Value
}
} return NEWOBJ
}

This approach is more recursive than a shallow copy of the traversal object, that is, whether the object's properties are also objects, recursive calls traverse the object until it is not an object.
But this way also does not consider the function, the REGEXP, the error and so on the type, needs more judgment, but the core thought also is recursively iterates over the object copy, moreover this way is slightly higher than the JSON deep copy efficiency.

OA Credit disk source code to build JS Copy method Jilin Fast three source for sale

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.