当前位置:网站首页>JS foundation -- object static method
JS foundation -- object static method
2022-07-25 02:48:00 【H5_ ljy】
List of articles
One 、Object Static methods
Static methods are methods that are called directly using the class name
object What are the static methods in ?
1.getPrototypeOf()
Object.getPrototypeOf() Get the prototype of the object , amount to obj._proto_
function fn () {
}
fn.prototype=["ljy",22]
var obj=new fn()
var re=Object.getPrototypeOf(obj)//==>obj.__prto__
console.log(re)

2.getOwnPropertyNames()
Object.getOwnPropertyNames() take obj The names of enumerable and non enumerable attributes of form an array and return
var obj=[10,203,4]
var re=Object.getOwnPropertyNames(obj)
console.log(obj,re)

3.keys()
Object.keys() Returns an array of enumerable properties of a given object , The property names in the array are arranged in the same order as those returned by normal loop traversal of the object .
var obj={name:'akren',age:20}
var re=Object.keys(obj)
console.log(re)

4.values()
Object.values() Method returns an array of all enumerable property values of the given object itself , Order and use of values for…in The order of the cycles is the same ( The difference lies in for-in Loop through the properties in the prototype chain ).
var obj={name:'ljy',age:22}
var re=Object.values(obj)
console.log(re)

5.defineProperty()
Object.defineProperty() Method defines a new property directly on an object , Or modify the existing properties of an object , And return this object .
You can define multiple values at once .
| The descriptor | effect |
|---|---|
| configurable | If and only if the configurable The key value is true when , Only the descriptor of this property can be changed , At the same time, this attribute can also be deleted from the corresponding object . The default is false. |
| enumerable | If and only if the enumerable The key value is true when , This property will appear in the enumeration property of the object . The default is false. |
| value | The value corresponding to this property . It can be anything that works JavaScript value ( The number , object , Functions, etc ). The default is undefined. |
| writable | If and only if the writable The key value is true when , The value of the property , That's the top value, Can be changed by the assignment operator . The default is false. |
| get | Attribute getter function , without getter, Then for undefined. When accessing this property , This function will be called . No parameters are passed in during execution , But it will pass in this object ( By inheritance , there this It does not have to be the object that defines the property ). The return value of this function will be used as the value of the property . The default is undefined. |
| set | Attribute setter function , without setter, Then for undefined. When the property value is modified , This function will be called . This method takes a parameter ( That is, the new value given ), The this object . The default is undefined. |
Case study 1:
var obj={}
var temp;
Object.defineProperty(obj,"ljy",{
set(arg){ // Set the stored data
temp=arg;
console.log(" You deposited "+arg) // When we save this attribute, we print (" You deposited "+arg)
},
get(){ // Get stored data
return temp+' year ' // When we get the data of this attribute, we return temp+' year '
}
})
obj.ljy=22
console.log(obj['ljy'])

Case study 2:
var obj={}
Object.defineProperty(obj,"ljy",{
enumerable:true,
value:666,
writable:false, //value Will not be changed by the assignment operator
})
obj.ljy=22
console.log(obj['ljy'])

6.create()
Object.create() Method creates a new object , Use existing objects to provide prototypes of newly created objects . Also known as factory functions .
var obj = Object.create([10,20,30],{'ljy':{ // The first parameter represents the prototype , The second parameter represents the object attribute and attribute value description defineProperty() equally .
value:22,
writable:false
}});
console.log(obj) // Printed Array The representative prototype is an array

7.getOwnPropertyDescriptor(obj,propName)
Object.getOwnPropertyDescriptor() Method returns a self owned property on the specified object (propName) The corresponding property descriptor .( Self owned attribute refers to the attribute directly assigned to the object , Properties that do not need to be looked up from the prototype chain )
var obj={name:"karen"}
var f1=Object.create(obj,{life:{
value:'ljy'
}})
console.log(f1)
var re=Object.getOwnPropertyDescriptor(f1,"life")
console.log(re)

8.preventExtensions(obj)
Object.preventExtensions() Method makes an object non extensible , That is, you can never add new attributes .
var obj={ljy:22}
Object.preventExtensions(obj)
obj.age=200
console.log(obj,obj.age)

9.seal()
Object.seal() Method to close an object , Prevent adding new properties and marking all existing properties as non configurable .
The value of the current property can be changed as long as it is writable .
var obj={ljy:22}
var re=Object.seal(obj)
obj.age=22
delete obj.ljy
console.log(obj)

10.freeze(obj)
It can be forbidden to obj All operations , It's relative to preventExtensible In terms of method , Attribute's configurable Properties and writable Properties are set to false.
var obj={ljy:22}
var re=Object.freeze(obj)
delete obj.ljy
obj.ljy=100
obj.age=20
console.log(obj)

11. Determine whether a method is set
Object.isExtensible(obj)、Object.isSealed(obj)、Object.isFrozen(obj) It corresponds to whether the above three methods are correct obj Set up
var obj={ljy:22}
var re=Object.freeze(obj)
delete obj.ljy
obj.ljy=100
obj.age=20
console.log(obj,Object.isFrozen(obj))

边栏推荐
- Wechat sports field reservation of the finished works of the applet graduation project (5) assignment
- IO (1) -io layering
- My creation anniversary (3rd Anniversary)
- Text reading end judgment
- Let's customize the loader
- Inheritance (prototype)
- Digital business cloud: how to realize the application value of supplier SRM management system?
- Jenkins configuration plug-in interface displays "suggestions collection" in Chinese
- Unity refers to a variable in another class (its own instance)
- BGP introduction
猜你喜欢

Technical experts from large factories: Thoughts on cloud nativity and software supply chain security

Example demonstration of "uncover the secrets of asp.net core 6 framework" [02]: application development based on routing, MVC and grpc

Request and response

Picgo configuring Alibaba cloud OSS

Arduino IDE for raspberry PI Pico development firmware localization installation tutorial

Creating elements of DOM series

Sword finger offer 11. rotate the minimum number of the array

C: wechat chat software instance (wpf+websocket+webapi+entityframework)

Explorer TSSD 2019 software installation package download and installation tutorial

C language: Structure -- a detailed explanation of memory byte alignment
随机推荐
Flink's study notes
Use unicloud cloud function to decode wechat motion steps in applet
Rotating frame target detection mmrotate v0.3.1 training hrsc2016 data set (II)
Sequence diagram of UML diagram series
Is it necessary to increase the number of milliseconds and save several KB of memory in the program?
Conceptual distinction between Po, Bo, VO, dto and POJO
Picgo configuring Alibaba cloud OSS
English grammar_ Reflexive pronoun
YuQue - a useful tool for document writing and knowledge precipitation
My creation anniversary (3rd Anniversary)
Daily three questions 7.16
Domestic edge computing organization and product research
"Introduction to interface testing" punch in to learn day07: websocket interface: how to test a completely unfamiliar protocol interface?
StrError and PERROR
Pycharm writes SQLite statements without code prompts
Pypi counts the number of Downloads
Common Oracle commands
DLL load failed: the page file is too small to complete the operation
Jenkins plug-in development -- plug-in expansion
Explorer TSSD 2019 software installation package download and installation tutorial