'use strict';
//We define an empty object.
var task = {};
//We define a property on the object task with key as 'text' and value as 'Get this job done!'
Object.defineProperty(task, 'text', {
value: 'Get this job done!'
});
console.log(task.text);
//*****************************************************************
var task = {};
Object.defineProperty(task, 'text', {
value: 'Get this job done!',
//By default writable : false. Therfore, we have to set it to true if want o make th property text writable
writable: true
});
/*
If the writable : true is commented out then by default writable : false and we will get TypeError: can not assign to read only property.
*/
task.text = task.text + ' ... Now!';
console.log(task.text);
//*****************************************************************
var task = {};
Object.defineProperty(task, 'text',
{
value: 'Get this job done!',
//enumerable is used in for in statements
enumerable: true
});
for(var f in task){
console.log(f);/* We get text on the console but if we do not set enumerable: false then by default
enumberable: false and nothing will be shown on the console.*/
}
//*****************************************************************
var task = {};
Object.defineProperty(task, 'text', {
value: 'Get this job done!',
configurable: true
});
Object.defineProperty(task, 'text', {
value: 'Done!'
});
console.log(task.text);/* The output on the console is 'Done!' as configurable: true but if we set
configurable: false or just delete that line then by default configurable:false and, therefore, we get
Ucaught TypeError: Cannot redefine property: text
*/
//*****************************************************************
//Getter functions
var task = {
_dueDate: '1/30/2016'
};
Object.defineProperty(task, 'dueDate', {
get: function(){
return this._dueDate;
}
});
console.log(task.dueDate);
//*****************************************************************
//Setter function
var task = {
_dueDate: '1/30/2016'
};
Object.defineProperty(task, 'dueDate', {
get: function (){
return this._dueDate;
},
set: function (newValue) {
this._dueDate = newValue;
}
});
//The line below will call the setter funciton
task.dueDate = '10/28/2016';
console.log(task.dueDate);
//*****************************************************************
//Assigning multiple properties to a single task
var task = {};
Object.defineProperties(task,
{
// Setting the property text of the object task
'text': {
value: 'New Task'
},
//setting the property dueDate of the object task
'dueDate': {
value: '1/30/2014'
}
}
);
console.log(task.text + ' Due: ' + task.dueDate);
var descriptor = Object.getOwnPropertyDescriptor(task, 'text');
console.log(descriptor);//Object {value: "New Task", writable: false, enumerable: false, configurable: false}
var descriptor = Object.getOwnPropertyDescriptor(task, 'dueDate');
console.log(descriptor);//Object {value: "1/30/2014", writable: false, enumerable: false, configurable: false}
//*****************************************************************
//*****************************************************************
Sunday, 3 April 2016
Defining custom properties on an object to have more control.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment