JavaScript Objects can be seen as a collection of properties. With the object literal syntax, The values are written as name:value pairs (name and value separated by a colon).
Properties are identified using key and value pairs. The Property values can be values of any type, including other objects, which enables building complex data structures, A key value is a String.
What’s a JavaScript Object Method?
JavaScript Methods are actions that can be performed on objects.
Methods are stored in properties as function definitions.
Methods are usually used to access and manipulate the data stored in an object.
3 Types of JavaScript Methods…
Object.create()
Object.keys()
Object.values()
Object.create() Method
This is a static method that creates a new object, using an existing object as the prototype of the newly created object.
eg:
const person = {
isHuman: false,
printIntroduction: function () { console.log(My name is ${
this.name
}. Am I human? ${this.isHuman}
); }, };
const me = Object.create(person);
me.name = 'Matthew';
// "name" is a property set on "me", but not on "person" me.isHuman = true;
// Inherited properties can be overwritten
me.printIntroduction();
// Expected output: "My name is Matthew. Am I human? true"
Object.keys() Method..
The object.keys() methods creates an array containing the keys of an object.
We can create an object and print the array of keys.
Eg:
// Initialize an object const employees = {
boss: 'Michael',
secretary: 'Pam',
sales: 'Jim',
accountant: 'Oscar' };
// Get the keys of the object const keys = Object.keys(employees);
console.log(keys);
expected output:
[‘’boss’’, ‘’secretary’’, ‘’sales’’, ‘’accountant’’]
Object.values() Method..
As this name implies this works just like the Object.keys() method, it returns an array containing the values of an object.
Eg:
// Initialize an object const session = { name: “James”, gender: “male” isHandsome: true};
// Get all values of the object const values = Object.values(session);
console.log(values);
expected output:
[‘’james’’, ‘’male’’, ‘’true’’]
Thanks for reading, kindly like and share this article so you can help others. Author: James ✍️