任何非原始类型的值(字符串,数字,布尔值,符号,null 或未定义)都是对象,数组和函数也不例外。
内置属性
静态方法
Object 实例和 Object 原型对象
JavaScript 中的所有对象都来自 Object;所有对象从 Object.prototype 继承方法和属性,尽管它们可能被覆盖。
Object.prototype.constructor //特定的函数,用于创建一个对象的原型。
实例方法
创建对象
const person = {}
typeof person //object
const person = Object()
typeof person //object
const person = new Object()
typeof person //object
const car = Object.create()
const person = {
age: 36,
name: 'funny',
speak: () => {
//speak
}
}
const person = Object({
age: 36,
name: 'funny',
speak: () => {
//speak
}
})
const person = new Object({
age: 36,
name: 'funny',
speak: () => {
//speak
}
})
const copied = Object.assign({}, target1, target2, ...target)
Object.assign-将一个或多个对象的所有可枚举属性复制到另一个对象中。浅拷贝,复制对象引用而非对象本身。
const original = {
name: 'Fiesta',
car: {
color: 'blue'
}
}
const copied = Object.assign({}, original)
original.name = 'Focus'
original.car.color = 'yellow'
copied.name //Fiesta
copied.car.color //yellow
const newObject = Object.create(prototype)
const newObject = Object.create(prototype, newProperties)
使用指定原型创建新的对象,可以指定第二个参数来向对象添加新属性
const animal = {}
const dog = Object.create(animal)
const animal = {}
const dog = Object.create(animal, {
breed: {
value: 'Siberian Husky'
}
})
console.log(dog.breed) //'Siberian Husky'
直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象
const dog = {}
Object.defineProperty(dog, 'breed', {
value: 'Siberian Husky'
})
console.log(dog.breed) //'Siberian Husky'
扩展:vue 原理之-神奇的 Object.defineProperty
直接在一个对象上定义新的属性或修改现有属性,并返回该对象
const dog = {}
Object.defineProperties(dog, {
breed: {
value: 'Siberian Husky'
}
})
console.log(dog.breed) //'Siberian Husky'
ES2017,返回自身可枚举属性的键值对数组
const person = {name: 'Fred', age: 87}
Object.entries(person) // [['name', 'Fred'], ['age', 87]]
const people = ['Fred', 'Tony']
Object.entries(people) // [['0', 'Fred'], ['1', 'Tony']]
冻结对象,冻结后不能再添加或修改
const dog = {}
dog.breed = 'Siberian Husky'
const myDog = Object.freeze(dog)
Object.isFrozen(dog) //true
Object.isFrozen(myDog) //true
dog === myDog //true
dog.name = 'Roger' //TypeError: 无法添加name属性,dog是不可扩展的
返回自由属性描述符
const dog = {}
Object.defineProperties(dog, {
breed: {
value: 'Siberian Husky'
}
})
Object.getOwnPropertyDescriptor(dog, 'breed')
/*
{
value: 'Siberian Husky',
writable: false,
enumerable: false,
configurable: false
}
*/
返回一个由指定对象的所有自身属性的属性名(包括不可枚举属性但不包括 Symbol 值作为名称的属性)组成的数组
const dog = {}
dog.breed = 'Siberian Husky'
dog.name = 'Roger'
Object.getOwnPropertyNames(dog) //[ 'breed', 'name' ]
返回一个给定对象自身的所有 Symbol 属性的数
const dog = {}
const r = Symbol('Roger')
const s = Symbol('Syd')
dog[r] = {
name: 'Roger',
age: 6
}
dog[s] = {
name: 'Syd',
age: 5
}
Object.getOwnPropertySymbols(dog) //[ Symbol(Roger), Symbol(Syd) ]
返回指定对象的原型(内部[[Prototype]]属性的
const animal = {}
const dog = Object.create(animal)
const prot = Object.getPrototypeOf(dog)
animal === prot //true
//如果对象没有原型,将返回null
Object.prototype //{}
Object.getPrototypeOf(Object.prototype) //null
判断两个值是否相等
Object.is(a, b)
判断一个对象是否是可扩展的,返回布尔值。任何对象都是可扩展,除非使用了 Object.freeze()、Object.seal()、Object.preventExtensions()
判断一个对象是否被冻结,返回布尔值
const dog = {}
dog.breed = 'Siberian Husky'
const myDog = Object.freeze(dog)
Object.isFrozen(dog) //true
Object.isFrozen(myDog) //true
dog === myDog //true
判断一个对象是否被密封,返回布尔值
const dog = {}
dog.breed = 'Siberian Husky'
const myDog = Object.seal(dog)
Object.isSealed(dog) //true
Object.isSealed(myDog) //true
dog === myDog //true
返回对象的自身可枚举属性组成的数组
const car = {
color: 'Blue',
brand: 'Ford',
model: 'Fiesta'
}
Object.keys(car) //[ 'color', 'brand', 'model' ]
阻止对象扩展,但可以删除属性
const dog = {}
dog.breed = 'Siberian Husky'
Object.preventExtensions(dog)
dog.name = 'Roger' //TypeError: Cannot add property name, object is not extensible
delete dog.name
dog //{ breed: 'Siberian Husky' }
与 Object.freeze()类似,但不会使属性不可写。仅阻止添加或删除属性。与 Object.preventExtensions()类似,但也不允许删除属性:
const dog = {}
dog.breed = 'Siberian Husky'
Object.seal(dog)
dog.breed = 'Pug'
dog.name = 'Roger' //TypeError: Cannot add property name, object is not extensible
对象自身的所有可枚举属性值的数组
const person = {name: 'Fred', age: 87}
Object.values(person) // ['Fred', 87]
const people = ['Fred', 'Tony']
Object.values(people) // ['Fred', 'Tony']
判断对象自身是否有某个值,返回布尔值
const person = {name: 'Fred', age: 87}
person.hasOwnProperty('name') //true
person.hasOwnProperty('job') //false
测试一个对象是否存在于另一个对象的原型链上
const Animal = {
isAnimal: true
}
const Mammal = Object.create(Animal)
Mammal.isMammal = true
Animal.isPrototypeOf(Mammal) //true
const dog = Object.create(Animal)
Object.setPrototypeOf(dog, Mammal)
Animal.isPrototypeOf(dog) //true
Mammal.isPrototypeOf(dog) //true
判断对象的属性是否可枚举,返回布尔值
const person = {name: 'Fred'}
Object.defineProperty(person, 'age', {
value: 87,
enumerable: false
})
person.propertyIsEnumerable('name') //true
person.propertyIsEnumerable('age') //false
const person = {name: 'Fred'}
person.toLocaleString() //[object Object]
const person = {name: 'Fred'}
person.toString() //[object Object]
返回指定对象的原始值
const person = {name: 'Fred'}
person.valueOf() //{ name: 'Fred' }
属性
方法
function push(fromArray, toArray) {
for (let i = 0, len = fromArray.length; i < len; i++) {
toArray.push(fromArray[i])
}
return toArray
}
var array1 = [1, 2, 3, 4, 5]
var array2 = [6, 7, 8, 9, 10]
var array3 = []
push(array1, array3)
push(array2, array3)
var array1 = [1, 2, 3, 4, 5]
var array2 = [6, 7, 8, 9, 10]
var array3 = [...array1, ...array2]
var array3 = Array.of(...array2, ...array1)
array3 // [1,2,3,4,5,6,7,8,9,10];
var array1 = [1, 2, 3, 4, 5]
var array2 = [6, 7, 8, 9, 10]
var array3 = array1.concat(array2)
// or
var array3 = [].concat(array1, array2)
var array1 = [1,2,3,4,5];
var array2 = [6,7,8,9,10];
var array3 = array2.reduce((newArray, item) => {
newArray.push(item);
return newArray;
), array1);