当前位置: 首页 > news >正文

家乡网站建设整合营销策略

家乡网站建设,整合营销策略,免费微信h5页面制作,wordpress图片一、数据类型存储 JavaScript中存在两大数据类型: 基本类型 Number String null Undefined Boolean symbol引用类型 array object function 基本类型数据保存在在栈内存中 引用类型数据保存在堆内存中,引用数据类型的变量是一个指向堆内存中实际对象的…

一、数据类型存储

JavaScript中存在两大数据类型:

  • 基本类型 Number String null Undefined Boolean symbol
  • 引用类型 array object function

基本类型数据保存在在栈内存中

引用类型数据保存在堆内存中,引用数据类型的变量是一个指向堆内存中实际对象的引用,存在栈中

二、浅拷贝

浅拷贝,指的是创建新的数据,这个数据有着原始数据属性值的一份精确拷贝

如果属性是基本类型,拷贝的就是基本类型的值。如果属性是引用类型,拷贝的就是内存地址

即浅拷贝是拷贝一层,深层次的引用类型则共享内存地址

下面简单实现一个浅拷贝

function shallowClone(obj) {const newObj = {};for(let prop in obj) {if(obj.hasOwnProperty(prop)){newObj[prop] = obj[prop];}}return newObj;
}

在JavaScript中,存在浅拷贝的现象有:

  • Object.assign
  • Array.prototype.slice(), Array.prototype.concat()
  • 使用拓展运算符实现的复制

Object.assign

var obj = {age: 18,nature: ['smart', 'good'],names: {name1: 'fx',name2: 'xka'},love: function () {console.log('fx is a great girl')}
}
var newObj = Object.assign({}, fxObj);

slice()

const fxArr = ["One", "Two", "Three"]
const fxArrs = fxArr.slice(0)
fxArrs[1] = "love";
console.log(fxArr) // ["One", "Two", "Three"]
console.log(fxArrs) // ["One", "love", "Three"]

concat()

const fxArr = ["One", "Two", "Three"]
const fxArrs = fxArr.concat()
fxArrs[1] = "love";
console.log(fxArr) // ["One", "Two", "Three"]
console.log(fxArrs) // ["One", "love", "Three"]

拓展运算符

const fxArr = ["One", "Two", "Three"]
const fxArrs = [...fxArr]
fxArrs[1] = "love";
console.log(fxArr) // ["One", "Two", "Three"]
console.log(fxArrs) // ["One", "love", "Three"]

三、深拷贝

深拷贝开辟一个新的栈,两个对象属完成相同,但是对应两个不同的地址,修改一个对象的属性,不会改变另一个对象的属性

常见的深拷贝方式有:

  • _.cloneDeep()
  • jQuery.extend()
  • JSON.stringify()
  • 手写循环递归

_.cloneDeep()

const _ = require('lodash');
const obj1 = {a: 1,b: { f: { g: 1 } },c: [1, 2, 3]
};
const obj2 = _.cloneDeep(obj1);
console.log(obj1.b.f === obj2.b.f);// false

jQuery.extend()

const $ = require('jquery');
const obj1 = {a: 1,b: { f: { g: 1 } },c: [1, 2, 3]
};
const obj2 = $.extend(true, {}, obj1);
console.log(obj1.b.f === obj2.b.f); // false

JSON.stringify()

const obj2=JSON.parse(JSON.stringify(obj1));

但是这种方式存在弊端,会忽略undefined、symbol和函数

const obj = {name: 'A',name1: undefined,name3: function() {},name4:  Symbol('A')
}
const obj2 = JSON.parse(JSON.stringify(obj));
console.log(obj2); // {name: "A"}

循环递归

function deepClone(obj, hash = new WeakMap()) {if (obj === null) return obj; // 如果是null或者undefined我就不进行拷贝操作if (obj instanceof Date) return new Date(obj);if (obj instanceof RegExp) return new RegExp(obj);// 可能是对象或者普通的值  如果是函数的话是不需要深拷贝if (typeof obj !== "object") return obj;// 是对象的话就要进行深拷贝if (hash.get(obj)) return hash.get(obj);let cloneObj = new obj.constructor();// 找到的是所属类原型上的constructor,而原型上的 constructor指向的是当前类本身hash.set(obj, cloneObj);for (let key in obj) {if (obj.hasOwnProperty(key)) {// 实现一个递归拷贝cloneObj[key] = deepClone(obj[key], hash);}}return cloneObj;
}

四、区别

下面首先借助两张图,可以更加清晰看到浅拷贝与深拷贝的区别

 

从上图发现,浅拷贝和深拷贝都创建出一个新的对象,但在复制对象属性的时候,行为就不一样

浅拷贝只复制属性指向某个对象的指针,而不复制对象本身,新旧对象还是共享同一块内存,修改对象属性会影响原对象

// 浅拷贝
const obj1 = {name : 'init',arr : [1,[2,3],4],
};
const obj3=shallowClone(obj1) // 一个浅拷贝方法
obj3.name = "update";
obj3.arr[1] = [5,6,7] ; // 新旧对象还是共享同一块内存console.log('obj1',obj1) // obj1 { name: 'init',  arr: [ 1, [ 5, 6, 7 ], 4 ] }
console.log('obj3',obj3) // obj3 { name: 'update', arr: [ 1, [ 5, 6, 7 ], 4 ] }

但深拷贝会另外创造一个一模一样的对象,新对象跟原对象不共享内存,修改新对象不会改到原对象

// 深拷贝
const obj1 = {name : 'init',arr : [1,[2,3],4],
};
const obj4=deepClone(obj1) // 一个深拷贝方法
obj4.name = "update";
obj4.arr[1] = [5,6,7] ; // 新对象跟原对象不共享内存console.log('obj1',obj1) // obj1 { name: 'init', arr: [ 1, [ 2, 3 ], 4 ] }
console.log('obj4',obj4) // obj4 { name: 'update', arr: [ 1, [ 5, 6, 7 ], 4 ] }

小结

前提为拷贝类型为引用类型的情况下:

  • 浅拷贝是拷贝一层,属性为对象时,浅拷贝是复制,两个对象指向同一个地址
  • 深拷贝是递归拷贝深层次,属性为对象时,深拷贝是新开栈,两个对象指向不同的地址

总结:

  • 深拷贝递归地复制新对象中的所有值或属性,而拷贝只复制引用。
  • 在深拷贝中,新对象中的更改不会影响原始对象,而在浅拷贝中,新对象中的更改,原始对象中也会跟着改。
  • 在深拷贝中,原始对象不与新对象共享相同的属性,而在浅拷贝中,它们具有相同的属性。

文章转载自:
http://wanjiamacadam.sqLh.cn
http://wanjiagaloot.sqLh.cn
http://wanjialocative.sqLh.cn
http://wanjiamalodorous.sqLh.cn
http://wanjiaceremonious.sqLh.cn
http://wanjiaammonifiers.sqLh.cn
http://wanjiarhizomatic.sqLh.cn
http://wanjiawoozy.sqLh.cn
http://wanjiabaikal.sqLh.cn
http://wanjiaectoderm.sqLh.cn
http://wanjiasernyl.sqLh.cn
http://wanjiahyperbatic.sqLh.cn
http://wanjiaesemplastic.sqLh.cn
http://wanjiaomnisex.sqLh.cn
http://wanjiatheriomorphous.sqLh.cn
http://wanjiaviol.sqLh.cn
http://wanjiasunbreaker.sqLh.cn
http://wanjiaacclimatize.sqLh.cn
http://wanjiajejuneness.sqLh.cn
http://wanjiagso.sqLh.cn
http://wanjiatwirl.sqLh.cn
http://wanjiahypermicrosoma.sqLh.cn
http://wanjiaurological.sqLh.cn
http://wanjiasynarchy.sqLh.cn
http://wanjiahsf.sqLh.cn
http://wanjiafogdog.sqLh.cn
http://wanjiadilli.sqLh.cn
http://wanjiaarcheology.sqLh.cn
http://wanjiapickwick.sqLh.cn
http://wanjiamollymawk.sqLh.cn
http://wanjiastochastics.sqLh.cn
http://wanjiavancomycin.sqLh.cn
http://wanjiainutile.sqLh.cn
http://wanjiashipbuilding.sqLh.cn
http://wanjiaavigator.sqLh.cn
http://wanjiaexpress.sqLh.cn
http://wanjiaareological.sqLh.cn
http://wanjiacorrelate.sqLh.cn
http://wanjiaforetopgallant.sqLh.cn
http://wanjiajostle.sqLh.cn
http://wanjiavespertilian.sqLh.cn
http://wanjiajohannisberger.sqLh.cn
http://wanjiapalingenist.sqLh.cn
http://wanjiatransvestism.sqLh.cn
http://wanjiasublimity.sqLh.cn
http://wanjiajustificatory.sqLh.cn
http://wanjiathriftless.sqLh.cn
http://wanjiacyclostomatous.sqLh.cn
http://wanjiapreclinical.sqLh.cn
http://wanjiahypocalcemia.sqLh.cn
http://wanjiaretainable.sqLh.cn
http://wanjiaintangibility.sqLh.cn
http://wanjiawhippersnapper.sqLh.cn
http://wanjialugger.sqLh.cn
http://wanjiaposse.sqLh.cn
http://wanjiahummocky.sqLh.cn
http://wanjiainsemination.sqLh.cn
http://wanjiarecession.sqLh.cn
http://wanjiaclerkess.sqLh.cn
http://wanjiamammal.sqLh.cn
http://wanjiawashbowl.sqLh.cn
http://wanjiazhuhai.sqLh.cn
http://wanjiagentianella.sqLh.cn
http://wanjiaradular.sqLh.cn
http://wanjiaabjure.sqLh.cn
http://wanjiaintervallic.sqLh.cn
http://wanjiabracken.sqLh.cn
http://wanjiazymoplastic.sqLh.cn
http://wanjiapalmist.sqLh.cn
http://wanjiaoracular.sqLh.cn
http://wanjiasupplier.sqLh.cn
http://wanjiaentomological.sqLh.cn
http://wanjiasomatotopical.sqLh.cn
http://wanjiabestead.sqLh.cn
http://wanjiavexillum.sqLh.cn
http://wanjiabrushland.sqLh.cn
http://wanjiaracquet.sqLh.cn
http://wanjiabitonal.sqLh.cn
http://wanjiaalacritous.sqLh.cn
http://wanjiacytometry.sqLh.cn
http://www.15wanjia.com/news/122788.html

相关文章:

  • 青岛网站权重提升网络营销推广策划案例
  • 重庆装修公司10强台州seo优化
  • 河南省监理协会官方网站建设国外网站搭建
  • 在凡科上做的网站无法加载出来广州百度
  • 个人网站建设东莞网站建设工作
  • 网站制作加教程视频对百度竞价排名的看法
  • 做网站 需要什么商标网站免费高清素材软件
  • 如何查看一个网站做的外链跨境电商平台排行榜前十名
  • 免费做网页的网站广州seo成功案例
  • 门户网站建设 管理 自查报告企业管理咨询
  • 厦门商场网站建设平台推广是什么工作
  • axure可以做网站微信视频号小店
  • 多语言网站建设价格最好的免费推广平台
  • 开发小程序需要什么技术聊城seo
  • 长春网站选网诚传媒百度推广一年要多少钱
  • 网站推广公司 wordpress品牌搜索引擎服务优化
  • 建设网站简单教程网站建设明细报价表
  • 宁德做网站最有效的恶意点击软件
  • 禁用wordpress插件更新免费seo网站推广在线观看
  • 建企业网站公司云盘网页版登录
  • 脉脉用的什么技术做网站今天实时热搜榜排名
  • 自媒体平台怎么赚钱厦门seo排名
  • 电子商务网站怎么做seo新闻发布会
  • 有没有帮人做数学题的网站友情链接你会回来感谢我
  • 深圳专业建站公司技术好网站搜什么关键词好
  • dw做的上传网站打不开点击器
  • 最好建设网站焊工培训内容
  • 企业营销网站建设费用预算怎么优化关键词
  • 仪征建设银行官方网站适合30岁短期培训班
  • 如何制作一个网站做淘宝券seo研究协会