Friday, January 6, 2023

JavaScript Objects

 // To hold unordered / unstructured data

const resource = {
	firstName : 'Srinu',
	lastName : 'SFDC',
    birthYear : 1987,
	//calcAge : function(birthYear) {
		//return 2021 - birthYear;
	//},
	//calcAge : function() {
    	//console.log(this);
		//return 2021 - this.birthYear;
	//},
	calcAge : function() {
		this.age = 2021 - this.birthYear;
		return this.age;
	},
	designation : 'Salesforce Architect',
	certifications: ['Admin','PD1','PD2','App Builder'],
	getDetailInfo : function() {
		return `${this.firstName} is a ${this.designation} with the age ${this.calcAge()} and this resource is having ${this.certifications.length} certifications.`;
	}
};
console.log(resource.getDetailInfo());
console.log(resource.calcAge())
console.log(resource.age);
//console.log(resource.calcAge());
//console.log(resource.calcAge(1993));
//console.log(resource['calcAge'](1987));

// Dot and Bracket notation
console.log(resource.firstName);
console.log(resource['lastName']);

const selected = prompt('Choose firstName or lastName or age or designation or certifications');
if(resource[selected]) {
	console.log(resource[selected]);
}

resource.city = 'Bangalore';
resource['country'] = 'India';
console.log(resource);

No comments:

Post a Comment