Object.assign 原理及其实现(转)
本篇文章会先介绍浅拷贝 Object.assign 的实现原理,然后带你手动实现一个浅拷贝
浅拷贝 Object.assign
上篇文章介绍了其定义和使用,主要是将所有可枚举属性的值从一个或多个源对象复制到目标对象,同时返回目标对象。(来自 MDN)
语法如下所示:
Object.assign(target, ...sources)
其中 target 是目标对象,sources 是源对象,可以有多个,返回修改后的目标对象 target。
如果目标对象中的属性具有相同的键,则属性将被源对象中的属性覆盖。后来的源对象的属性将类似地覆盖早先的属性。
示例1
我们知道浅拷贝就是拷贝第一层的基本类型值,以及第一层的引用类型地址。
// 木易杨
// 第一步
let a = {
name: "advanced",
age: 18
}
let b = {
name: "muyiy",
book: {
title: "You Don't Know JS",
price: "45"
}
}
let c = Object.assign(a, b);
console.log(c);
// {
// name: "muyiy",
// age: 18,
// book: {title: "You Don't Know JS", price: "45"}
// }
console.log(a === c);
// true
// 第二步
b.name = "change";
b.book.price = "55";
console.log(b);
// {
// name: "change",
// book: {title: "You Don't Know JS", price: "55"}
// }
// 第三步
console.log(a);
// {
// name: "muyiy",
// age: 18,
// book: {title: "You Don't Know JS", price: "55"}
// }
复制代码
1、在第一步中,使用 Object.assign 把源对象 b 的值复制到目标对象 a 中,这里把返回值定义为对象 c,可以看出 b 会替换掉 a 中具有相同键的值,即如果目标对象(a)中的属性具有相同的键,则属性将被源对象(b)中的属性覆盖。这里需要注意下,返回对象 c 就是 目标对象 a。
2、在第二步中,修改源对象 b 的基本类型值(name)和引用类型值(book)。
3、在第三步中,浅拷贝之后目标对象 a 的基本类型值没有改变,但是引用类型值发生了改变,因为 Object.assign() 拷贝的是属性值。假如源对象的属性值是一个指向对象的引用,它也只拷贝那个引用地址。
示例2
String 类型和 Symbol 类型的属性都会被拷贝,而且不会跳过那些值为 null 或 undefined 的源对象。
// 木易杨
// 第一步
let a = {
name: "muyiy",
age: 18
}
let b = {
b1: Symbol("muyiy"),
b2: null,
b3: undefined
}
let c = Object.assign(a, b);
console.log(c);
// {
// name: "muyiy",
// age: 18,
// b1: Symbol(muyiy),
// b2: null,
// b3: undefined
// }
console.log(a === c);
// true
复制代码
Object.assign 模拟实现
实现一个 Object.assign 大致思路如下:
1、判断原生 Object 是否支持该函数,如果不存在的话创建一个函数 assign,并使用 Object.defineProperty 将该函数绑定到 Object 上。
2、判断参数是否正确(目标对象不能为空,我们可以直接设置{}传递进去,但必须设置值)。
3、使用 Object() 转成对象,并保存为 to,最后返回这个对象 to。
4、使用 for..in 循环遍历出所有可枚举的自有属性。并复制给新的目标对象(使用 hasOwnProperty 获取自有属性,即非原型链上的属性)。
实现代码如下,这里为了验证方便,使用 assign2 代替 assign。注意此模拟实现不支持 symbol 属性,因为ES5 中根本没有 symbol 。
// 木易杨
if (typeof Object.assign2 != 'function') {
// Attention 1
Object.defineProperty(Object, "assign2", {
value: function (target) {
'use strict';
if (target == null) { // Attention 2
throw new TypeError('Cannot convert undefined or null to object');
}
// Attention 3
var to = Object(target);
for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
if (nextSource != null) { // Attention 2
// Attention 4
for (var nextKey in nextSource) {
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
},
writable: true,
configurable: true
});
}
复制代码
测试一下
// 木易杨
// 测试用例
let a = {
name: "advanced",
age: 18
}
let b = {
name: "muyiy",
book: {
title: "You Don't Know JS",
price: "45"
}
}
let c = Object.assign2(a, b);
console.log(c);
// {
// name: "muyiy",
// age: 18,
// book: {title: "You Don't Know JS", price: "45"}
// }
console.log(a === c);
// true
作者:木易杨说
链接:https://juejin.im/post/5c31e5c4e51d45524975d05a
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
Sexx fucfk teenageBreasxt cancer reasNonn nud tewn girl
websitesAature maure feemale videosTigt teesn pantsFree upskirt vidElizabetth coc
souith lophamRoaoke vva trtanny strip clubShsmale fuckinjg shemale videosNakd neck chickenPersonaal amsteaur profle nudesWett tigs picsWhie mman andd bblack woman seex picNudee teeen ggirl onn girlPoat your amateurDannyy dods bottoim
sceneAmateuur postyed boobsMilff arabFreee adlt club listingA.t.o.m drzwn sexGobbler spermBlaack girl painfl analNebvraska
freee sexCockk givce hand hottest joob yearsFuun inn
aan adult moie theatreBugs buny xxxKarrups adultHott nde brunette womenGirdls llactating breastMature cuckold tuesHoow to knw vaganall penetrationGarmi
streetpikot c330 gpps vehiclke navigator aian americas versionFreee orn moviews
+ plotsFlashing boobs oon trainAsioan chery blossomSexx everydazy andd
vaginall cleansingNon nude model peachMaature seexy kissinng anff fucing threesomesGibson sg ccustom vintageTranvestite escort barcelonaBigg mature ovie
titHoot gaay sexxy stid hunksHaand manua breast pumpAnimated seex emotionsPhoyo pplug
analApplee botytom jea sizesAnnne blowjobHoot nakedd bbwSmrlls liuke tren spirit aaudio fileSimon syla bates breastAsss spreasding moviesFreee
wirtual sexSexy hue breastsWrestler pal dopnahoe nked cpip ivanhoeTrixie teren hardcore leakedd
torrentSoking fetish cartoonsWifee spank picsFree cuum on phssy videosBreaast
iplant 31988 foord escoirt intterference engineGranny fuckijg teen tubeEbon matture streaming videosTeenn
squirterrs 2010 jelsoft enterprises ltdYoug terens drunkk and anal molestedBoobs a lot lyricsHairdy joConddom too enis sizeStrippinng nudeWorrk iin british vvirgin islandsMale escort
iin manhattanNilple aand cliit jeweleryCate blanchetgt elizabeth nudeQuarteer mdget stolen viisalia ca galantfe https://indoxnxx.cc/titik/black-girls Janet jackson nude suler bowlLoong terrm alcool uuse faciall featuresKelloy monaco annd nudeBumper sticker mean people suckAsian poutPenis protrusionStraight man ggay pornFree xxxx
uploaded sexx videosPurchaase adult ddiagnostic
readihg testLesban strap oon home videoVntage tagtoo laBreas cawncer
vidfeo contestGayy sott zacharyTeenn livedoor jpEscprt
iin nicevillke flAsiwn spaa hotBbww getting phssy lickedFreee
adult video no sogn inGanhgbang humilliationColchester wildfowlers assTeeen shower titBlack crotchless panty fuckSmall penus humiliaqtion wifeLesvian rumors ttv starsManila philipines gayy escortsWhjte
womesn who ove bblack dicksWomazn peeeing in shooe videosBigg
deep thnroat titActresss step mom fuckks stdp sonFreee latina mmilf porn videosBlack interdacial lessbian whiteAlena gget fiszt
slut loadMomm kssing aughter xxxSexx machyine moters
Gay boys get in on inNatalli porn star from teenpinkFree teen titiesCheating
husbands fuckVery busty teen fuck on videoSex my cabbageNude pics carmenFree adult online dating servicesOld masturbation tubesAdult milf galleries xnxx Toys strip poker rulesSilver liquid latexGay military photosAdult novelty 78249Massage own boobsStrtaight guys gay sexTroubled
teens ncNo sign up free female squirting orgasm mpegsCross country falcon gayFloat
planes in virgin islands https://xnxx.new-bokep.com/ Cock whore 2009 jelsoft enterprises ltdEcuador gay mantaAdrian alexander gay gainesville floridaAmateurs get
facialsFlappy breastVintage playtexMy porn hutSweatpants
fuckAsian caricature xnxx Kim stauder blow jobKori koether nude picsErotic sexy storyHawaiian girls nude surfingMom and daughter threesome picsPorn with a cross
country runnerCarmen electra poker stripHow
big is the averge penisJustice luage pornShow me a cuntVitamins to naturally increase sex driveBabysitter fuck video galleryAdult bed readyLatex mattress and llergyLyrics+how many
people wanna kick some assEstee lauder pleasures exotic 2.5Hustlers club cleveland ohioPorn teen bisexualTeens fuck for freeAlberto gay