Learn about the object method before proxy

Object.getPrototypeOf()

Returns the prototype of the specified object

var pro = {
    
    }
var obj1 = Object.create(pro)
console.log(Object.getPrototypeOf(obj1)==pro)

//true

Object.setPrototypeOf()

Set the prototype of a specified object to another object or null

Object.setPrototypeOf(obj,prototype)

Parameter
obj: for the object whose
prototype needs to be changed prototype: the prototype specified for obj

Object.isExtensible()

Determine whether an object can be extended (whether new attributes can be added)

var obj1={
    
    }
Object.preventExtension(obj1)//变为不可扩展
console.log(Object.isExtensible(obj1))   //false

var sealObj ={
    
    }
obj2 = Object.seal(sealObj) //密封对象不可扩展
console.log(Object.isExtensible(sealObj))  //false

var frozenObj = {
    
    }
forzenObj = Object.freeze(frozenObj)
console.log(Object.isExtensible(frozenObj)) 

Object.preventExtension

Is an object becoming non-extensible, the specific use is shown in isExtensible

Object.getOwnPropertyDescriptor

Returns a free attribute descriptor of the specified object, the first parameter is the specified object, and the second parameter is the attribute of the descriptor to be obtained

var obj= {
    
    }
Object.defineProperty(obj,"keyOne",{
    
    
	value:"Hello world",
	writable:false,  //属性不由运算符赋值
	enumerable:false	//属性不可枚举
})
console.log(Object.getOwnPropertyDescriptor(obj,"keyOne"))
// { value: "Hello world", writable: false, enumerable: false, configurable: false }

Object.defineProperty

Add an attribute and its descriptor to an object, the first parameter is the specified object, the second parameter is the attribute name, and the third parameter is the descriptor object

Descriptor
attribute value The default value of the attribute value is undefined,
whether the attribute can be modified by the operator, the default value is false,
whether the enumerable attribute is enumerable, the default value is false,
whether the configurable attribute can be deleted, the default value is true

var obj ={
    
    
	keyOne:"hey"
}
Object.defineProperty(obj,"keyTwo",{
    
    
	value:"Hellow World",
	writable:false,
	enumerable:false,
	configurable:true
})
//对obj对象新增了一个属性keyTwo,该属性值为"Hello World",不可修改,不可枚举,可被删除,接下里我们一个一个试一下

console.log(obj.keyTwo)   //Hello World

obj.keyTwo="bye bye"
console.log(obj,keyTwo)     //Hello World

for(let key in obj){
    
    
	console.log(key)		//keyOne
}
delete obj.keyTwo
console.log(obj.keyTwo)		//undefined

Object.keys

Returns an array of enumerable properties of the specified object

var obj={
    
    
	keyOne:0,
	keyTwo:1,
	keyThree:2
}
console.log(Object.keys(obj))	//[ 'keyOne', 'keyTwo', 'keyThree' ]

Object.getOwnPropertyNames()

Returns an array of the properties of a specified object, including enumerable and non-enumerable properties

var obj={
    
    
    keyOne:0,
    keyTwo:1,
    keyThree:2
}
Object.defineProperty(obj,"keyFour",{
    
    
    value:4,
    enumerable:false
})
console.log(Object.keys(obj)) 	//[ 'keyOne', 'keyTwo', 'keyThree' ]

console.log(Object.getOwnPropertyNames(obj))	//[ 'keyOne', 'keyTwo', 'keyThree', 'keyFour' ]

Object.getOwnPropertySymbols

Returns an array of all Symbol attributes of the specified object

var symbolKey1 = Symbol("keyOne")
var symbolKey2 = Symbol.for("keyTwo")
//symbol() 和 symbol.for()的区别是   symbol不会检索全局 每次都创建一个新的symbol类型值,symbol.for(key)会先检索全局,如果key已存在则会返回已存在的symbol类型值,不存在的话则会创建一个新的symbol值

var obj={
    
    }
obj[symbolKey1]="symbolkeyOne"
obj[symbolKey2]="symbolKeyTwo"
coconsole.log(Object.getOwnPropertySymbols(obj))  //[ Symbol(keyOne), Symbol(keyTwo) ]

Guess you like

Origin blog.csdn.net/weixin_38987500/article/details/114097182