Getters & Setters

Video

JavaScript Notes

JavaScript
    // es6-object methods
    // New ways of declaring methods for objects
    // getters and setters for object properties
    let log = console.log;
    let x = 7;
    
    let obj = {
    	_prop1: 1979,
    	get prop1( ) {
    	      return this._prop1;	// get returns another property
    	},
    	set prop1(_val) {
    	      this._prop1 = _val;	// 
    	},
    	prop2: 'Alien',
    	x,					        // a new property with the value of a variable can also be declared as follows
    	prop3( ) {				    // function declaration can be abbreviated to just its name
          	      log('called prop3');
    	}
    };
    
    log(obj.x);
    log(obj.prop1);					// returns prop1 by launching the get method
    obj.prop1 = 1980 ;  or obj.prop1(1980);		// sets prop1 to 1980 through the set method
    log(obj.prop1);					// returns prop1 by launching the get method
    
    // Old Way
    /*var log = console.log;
    var obj = {
    	prop1: 1979,
    	prop2: ‘Alien’,
    	prop3: function ( ) {
    	}
    };*/