Properties In Javascript By Sagar Jaybhay 2020

In this article we will understand Properties in Javascript. How to write properties in Javascript and What is the use for Properties In JavaScript.

In object-oriented programming languages class have 3 types of properties

  1. Read/ Write properties
  2. Read-only properties
  3. Write only properties

class Employee
{

string _name;
int _salary;

public Employee(string name,int salary)
{
    _name=name;
    _salary=salary;
    
}
//read/write proerties
public int salary{
    get{return _salary;}
    set{_salary=value;}
}

//read only property
public string name{get;}

//write only property
public string email{set;}
}

Why we need properties?

Encapsulation is one of the pillar of object-oriented programming. Properties provide encapsulation. If you provide public fields you can not have more control over it but if you use properties then you have control.

function Employee(salary)
{
    this.Salary=salary;
}

var emp=new Employee(-1);

In the above example, we are setting salary =-1 which is wrong to overcome this kind of situation we required properties.

function Employee(name,salary)
{
    var _name=name;
    var _salary=salary;

    Object.defineProperty(this,"salary",{
        get:function(){
            return _salary;
        },
        set:function(value){
            if(value>0&&value<100000)
            _salary=salary;
            else
            alert("Invalid Salary;")
        }
    });

    Object.defineProperty(this,"name",{
        get:function(){
                return _name;
            }
    });

}

var emp=new Employee("Sagar Jaybhay",-1);
emp.salary=-1; //not set salary because we use conditions in that properties
emp.name="Xyz"; // not able to set the xyz name

alert(emp._name+" "+emp.salary)

To declare read and write properties in JavaScript use below code

 Object.defineProperty(this,"salary",{
        get:function(){
            return _salary;
        },
        set:function(value){
            if(value>0&&value<100000)
            _salary=salary;
            else
            alert("Invalid Salary;")
        }
    });

In this when we want to declare property we need to use Object.defineProperty method needs to use. In this, we have got and set functions to make property read and write.

To make property readonly then use the below code.

Object.defineProperty(this,"name",{
        get:function(){
                return _name;
            }
    });


GitHub :- https://github.com/Sagar-Jaybhay/JavaScript-All-Labs


Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

Sagar Jaybhay

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

You may also like...