当前位置:网站首页>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))

边栏推荐
- Physical experiment simulation
- Wechat sports field reservation of applet completion works applet graduation design (8) graduation design thesis template
- Work arrangement of generator set
- After six years of precipitation, new consumption has found a digital transformation paradigm
- Mid year summary and personal feelings
- Flink's study notes
- Jedispoolconfig parameter configuration from the perspective of source code
- Use unicloud cloud function to decode wechat motion steps in applet
- My creation anniversary (3rd Anniversary)
- ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience
猜你喜欢

How to take the mold for the picture of 1.54 inch TFT st7789 LCD screen

Coal industry supply chain centralized mining system: digitalization to promote the transformation and upgrading of coal industry

Custom types in C language

Generator set work arrangement problem code

Is it necessary to increase the number of milliseconds and save several KB of memory in the program?

Flink's study notes

Wechat sports field reservation of the finished works of the applet graduation project (6) opening defense ppt

Visualization of correlation coefficient matrix

Solution to the occupation of project startup port

The latest interview questions and analysis of software testing in 2022
随机推荐
A bit of knowledge - websites about scripts
Experienced the troubleshooting and solution process of an online CPU100% and the application of oom
BMW I3 based on clar architecture is not a simple "oil to electricity" product
If there is a segment in the encryption field, are you "bronze" or "King"?
Request and response
Keil compile download error: no algorithm found for: 08000000h - 08001233h solution
Generator set work arrangement problem code
What are the basic skills of engineers? How to practice? -- Learning experience sharing "suggestions collection"
Pypi counts the number of Downloads
Threat report in June: new bank malware malibot poses a threat to mobile banking users
Unity refers to a variable in another class (its own instance)
Large number processing -- use case
Execution methods with static code blocks and child and parent classes
Resolved (the latest version of selenium reported an error) attributeerror: module 'selenium webdriver‘ has no attribute ‘PhantomJS‘
"Introduction to interface testing" punch in to learn day09: Micro service interface: how to use mock to solve chaotic call relationships
Project management tool Zen
QT realizes calendar beautification
How to use blender to make 360 degree panorama and panoramic video?
Daily three questions 7.16
Pagoda workman WSS reverse proxy socket legal domain name applet chat remove port