foreach-keys
Object.keys(obj).forEach(function(key){
console.log(key,obj[key]);
}
for-in
for(var key in obj) {
console.log(key,":",obj[key]);
}
es6 entries-for-of
for (let [key, value] of Object.entries(obj)) {
console.log(`${key}: ${value}`);
}
es6 entries-foreach
Object.entries(obj).forEach(function (item) {
var [key, value] = item
console.log(key, ":", value)
})
var obj = Object.assign(o1, o2, o3)
// o1 也会被改变
// obj === o1 === { a: 1, b: 2, c: 3 }
// https://stackoverflow.com/a/455366
// in 会查找在原型链中的属性
console.log(‘toString‘ in obj)
// true
// hasOwnProperty 只会查找直接的属性
console.log(obj.hasOwnProperty(‘toString‘))
// false
// 浅拷贝
var copy = Object.assign({}, obj)
// 深拷贝
JSON.parse(JSON.stringify(obj))
es6
Object.is(window, window)
// true
Object.keys(obj)
Object.values(obj)
es6
var obj = {
userName: ‘1userName‘,
age: ‘2age‘,
gender: ‘3sexual‘,
}
const {
userName, // 基本解构操作
age,
gender: sex, // 解构更名
phone = ‘123‘, // 解构缺省值 phone: number = ‘123‘
} = obj
console.log(
userName,
age,
sex,
phone,
)
标签:Javascript
原文:https://www.cnblogs.com/simplue/p/14401022.html