Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

javascript的Object(对象) #28

Open
zhn4 opened this issue Feb 28, 2018 · 0 comments
Open

javascript的Object(对象) #28

zhn4 opened this issue Feb 28, 2018 · 0 comments

Comments

@zhn4
Copy link
Owner

zhn4 commented Feb 28, 2018

《你不知道的JavaScript》

对象

类型:
基本类型:String,Number,Boolean,null,undefined
引用类型:Object(Function,Array,Object,RegExp,Date,Error)

内置对象:String,Number,Boolean,Object,Function,Array,Date,RegExp,Error

复制对象(拷贝对象)

参考:

demo:http://jsbin.com/rekedirufa/edit?js,console,output

// 一个方法是用函数实现对象的属性复制
function shallowCopy(src) {
  var dst = {}
  for (var prop in src) {
    dst[prop] = src[prop]
  }
  return dst
}
// 另一个方法使用Object.assign({}, _需要复制的对象名_)
var a = {
  mark: 88,
  arr: [1, 2, 3]
}
var d = Object.assign({}, a)
  • 深复制(深拷贝)

demo:http://jsbin.com/rawanuqapi/edit?js,console,output
参考
concat():https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/concat ,concat() 方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。
slice():https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/slice ,slice() 方法返回一个从开始到结束(不包括结束)选择的数组的一部分浅拷贝到一个新数组对象。且原始数组不会被修改。

// 使用Array.concat()和Array.slice(0),这2个方法会返回一个新数组,返回新数组就意味这新的内存地址
var arr = [1, 2, 3, 4]

var arr2 = arr

var arr3 = arr.slice(0) //.slice()方法会返回新的数组,那就指向新的内存地址

arr2[0] = 99

console.log(arr)
console.log(arr2)
console.log(arr3)

var arr4 = arr.concat() //.concat()方法会返回新的数组,那就指向新的内存地址
arr4[0] = 100
console.log(arr4)

特殊情况
demo:http://jsbin.com/lagibirubo/edit?js,console,output

var arr = [1,2,3,[11,22]]

var arr2 = arr

var arr3 = arr.concat()

arr2[0] = 99
arr2[3][1] = 'asdf'

console.log(arr) // [99, 2, 3, [11, "asdf"]]
console.log(arr2) // [99, 2, 3, [11, "asdf"]]
console.log(arr3) // [1, 2, 3, [11, "asdf"]]

// arr2只是复制引用了arr,内存地址不变,所以修改99会改变
// arr3虽然是全新的内存地址,但是内部的数组还是复制引用,也就是说arr,arr2,arr3三个数组的[11,22]还是相同的内存地址

使用JSON.parse()和JSON.stringify()方法,复制对象指向新的内存地址
demo:http://jsbin.com/yizomexuyu/edit?js,console,output

var data = {
  arr: [1, 2, 3, 4] // 内存地址addr1
}

console.log(data)

var data2 = JSON.parse(JSON.stringify(data)) // 内存地址addr2

console.log(data2)

console.log('---下面是深拷贝的对比')

data2.arr[0] = 99

console.log(data)

console.log(data2)

console.log('---下面是浅拷贝的对比')

var data3 = Object.assign({}, data) // 内存地址addr1

data3.arr[3] = 66666

console.log(data)
console.log(data2)
console.log(data3)

// 使用JSON.parse()和JSON.stringify()方法,复制对象指向新的内存地址

类,实例概念类似房屋建造,类是蓝图,房屋是实例

@zhn4 zhn4 changed the title JavaScript的Object(对象) javascript的Object(对象) Feb 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant