Professional Documents
Culture Documents
JS Answer
JS Answer
JS Answer
language( C++, Java etc ). Some developers skip this part but it is a important part of language that we should be
aware of.
Table Of Contents
1. What is Object in JS
2. Property v/s Method
3. Property Descriptors
4. Writable Descriptors
5. Enumerable Descriptor
6. Immutability
7. Implementing Iterator to traverse Oject
Don't forget to follow me on linkedin to stay updated with the latest articles and insights! Let's connect and continue
our journey of learning and exploring together. Follow me on linkedin and join the conversation today!
1.
What is Object in JS?
The Object is one of JavaScript's data types. It is used to store data in key-value pairs. We can add properties to
object using the '.' operator or '.defineProperty()' method. We can use 'delete' operator to remove any property from
the objects.
obj.a = 4;
console.log(obj.a); // 4
delete obj.a;
NOTE :- The 'delete' operator only remove the property from object, it doesn't free memory
1. Literal Form
2. Constructed form ( Using the Object() constructor )
// literal form
var obj1 = {
a : 2,
b : 3
};
// constructed form
obj2.a = 3;
obj2.b = 4;
Built-in Objects.
1. String 5. Function
2. Number 6. Array
3. Boolean 7. RegEx
4. Object 8. Error
5. Date
Above given are actually built-in function constructors which can be used to create objects using 'new' operator.
NOTE :- Functions and Arrays are also objects in JS and 'null' is not a object, rather it is a
primitive type in Js.
function fun(){
console.log('hello world');
function fun(){
console.log('hello world');
var b = null;
console.log(b===null); // true
2.
Property v/s Methods.
Methods are functions which can be only accessed by the objects. But functions in JS are not owned by any object,
they only contain the reference of the function inside of them. So, functions inside objects are merely just another
'property' not 'method'. Generally they can be used interchangeably but always remember there is a slight difference.
var obj = {
fun : function(){
console.log('hello world')
};
var newFun = obj.fun; // reference of fun is passed
3.
Property Descriptors
As the name suggests, it give description of object's property. There are 4 basic properties :
NOTE :- (configurable : false) prevents delete operator from removing the property from object.
value : 2,
enumerable : true,
configurable : true,
writable : true
});
console.log(obj.a); // 2
console.log(Object.getOwnPropertyDescriptor(obj,'a'));
4.
Writable Descriptor
Writable descriptor is a boolean value which tells us if the value can be changed or not.
NOTE :- Even when writable is false Ojbect.defineProperty() can change the value of property but
'.' operator can't.
var obj = { a : 2 };
Object.defineProperty(obj,'a',{
writable : false,
});
obj.a = 8;
console.log(obj.a);
Object.defineProperty(obj,'a',{
value : 5,
});
console.log(obj.a);
5.
Enumerable Descriptor
Enumerable Descriptor is a boolean value which tell if the property should be visible during iteration.
Setting the enumerable descriptor to false, prevents the property from being accessed during iteration but can be
accessed using the '.' operator.
var obj = {
a : 2,
b : 3,
c : 4
};
Object.defineProperty(obj,'a',{
enumerable : false,
});
console.log(obj); // [ 'b','c' ]
console.log(obj.a);
6.
Immutability
An immutable value is one whose content cannot be changed. This is very useful in JavaScript as this can be used to
store state of application. Only when entirely new object is created we say that the state has changed.
We can only implement shallow component. This means that if the property is an object then its reference would be
immutable not it's content.
var obj = { a : 2 };
Object.preventExtension( obj );
3-> Seal
preventExtension() + configurable false
New properties can't be added
Existing properties can't be removed but value can be changed
var obj = {
a : 2,
b : 3,
c : 4
};
Object.seal(obj);
console.log(obj.d);
4-> Freeze
var obj = {
a : 2,
b : 3,
c : 4
};
Object.freeze(obj);
7.
Implementing iterator
let obj = {
a : 2,
b : 4,
c : 5
Object.defineProperty(obj,'my_iterator',{
writable : false,
enumerable : false,
configurable : false,
value : function(){
var idx = 0;
var ks = Object.keys(self);
return {
next : function(){
return {
value : self[ks[idx++]],
done : (idx>ks.length)
});
var it = obj['my_iterator']();
That is it for this blog. If you learned something from this blog then please like and share. If I made a mistake in this
blog then please comment I would surely get back to it.
I am planning to write more blogs like this, so stay tuned.
Don't forget to follow me on LinkedIn to stay updated with the latest articles and insights! Let's connect and continue
our journey of learning and exploring together. Follow me on LinkedIn linkedin and join the conversation today!