How to create an Object in the Javascript?
š Want to master this with real projects? Join the Playwright Automation Mastery course at The Testing Academy.
In this tutorial, We are going to understand what is Object and how we can create Objects in the javascript. What is Object? An object is a collection of properties, and a property is an association between a name (or key) and a value. A property’s value can be a function, in which case the property is known as a method.
//Start with
var coffeeObject = {
};
Now we write the properties for it.
var coffeeObject = {
color : 'blue',
plateType : 'Flat',
isHot : true,
temperatureInC : '96',
};
An Object can have method associated, Let’s have function/method that can be used to convert the temperature in C to F and attached to our coffeeObject.
[quads id=”3″]
var coffeeObject = {
color : 'blue',
plateType : 'Flat',
isHot : true,
temperatureInC : '96',
convertIntoF : function(coffeeObject.temperatureInC){
console.log('Temperature in C'+coffeeObject.temperatureInC);
temperatureInF = coffeeObject.temperatureInCĆ 9/5 + 32;
console.log('Temperature in F'+temperatureInF);
}
};
console.log("Cofee Mug Color :- "+ coffeeObject.color);
console.log("Cofee plateType :- "+ coffeeObject.plateType);
console.log("Cofee isHot :- "+ coffeeObject.isHot);
coffeeObject.convertIntoF();
[quads id=”4″]
To access the Object key, we have to use the dot pattern. e.g object.key.
Please note you can also access the object key’s value by
console.log(coffeeObject['color']);That’s all folks in the next tutorial we are going to learn more about the object oriented programming in javascript.
[quads id=”5″]
š Master Playwright End to End
Join hundreds of SDETs building real automation frameworks. Lifetime access, hands-on projects, and a job-ready portfolio.
