Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created October 11, 2017 17:33
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save prof3ssorSt3v3/b38039f992f9ab37f1e4c11350dbf65d to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/b38039f992f9ab37f1e4c11350dbf65d to your computer and use it in GitHub Desktop.
/***************************************
Property Descriptors Methods and Usage
Object.defineProperty(obj, propName, {} )
Object.defineProperties(obj, props)
Object.getOwnPropertyNames(obj)
Object.getOwnPropertyDescriptor(obj, prop)
Object.getOwnPropertyDescriptors(obj)
Object.keys(obj) - list of enumerable properties
Object.values(obj) - list of enumerable prop values
obj.propertyIsEnumerable(prop)
obj.hasOwnProperty(prop)
Objects can be
1. Extensible - new properties added
2. Frozen - props cannot be changed in any way
3. Sealed - props can't be deleted or configured
but are still writable
Object PROPERTIES can be
1. Writable - change the value
2. Enumerable - seen through a for...in loop
3. Configurable - change the property descriptors
Object.isExtensible(obj)
Object.isFrozen(obj)
Object.isSealed(obj)
Object.preventExtensions(obj)
Object.freeze(obj)
Object.seal(obj)
Descriptor Groups
DATA ACCESSOR
value get
writable set
configurable configurable
enumerable enumerable
****************************************/
let log = console.log;
let obj = {
name: 'Bob',
age: 45
};
Object.defineProperty(obj, 'test', {
value: 'Shagadelic',
writable: true,
configurable: true,
enumerable: false
} );
Object.defineProperty(obj, 'frank', {
configurable:true,
enumerable: true,
get: () => this.value,
set: (_val) => {
this.value = _val + " baby!";
}
});
for( let prop in obj){
log(prop);
}
log( obj, obj.test, obj.frank );
obj.frank = 'Shagadelic';
log(obj.frank);
@anandsingh1606
Copy link

thanks, It is very helpful!!

@AnujJha-stack
Copy link

thanks

@ProspektsMarch22
Copy link

Perfect! Thanks for the awesome content!

@IbrahimElgazwy
Copy link

Thanks and excellent

@mahes1287
Copy link

thank you

@noobDeveloper-dot-com
Copy link

Priceless

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment