Sitemap

JavaScript Quickly: Objects

3 min readDec 31, 2024

--

Hey, did you know JavaScript is all about objects?

JavaScript is an object-oriented programming (OOP) language. This means, the programming logic of JavaScript is one that breaks down problems into smaller, reusable groupings of data such as attributes and procedures/methods that can be used as a blueprint to create “objects”. And objects can be used to build data and functions.

Press enter or click to view image in full size

Inheritance

JavaScript objects inherit properties (and methods) from the Object.prototype. Objects with null prototypes cannot inherit from Object.prototype.

class Cat{
furColor;
coatPattern;
name;

sayMeow(){
return this.lives, "Meows"
}
}

function myCat(){
let myCat = new Cat();
myCat.furColor="black";
myCat.coatPattern="solid";
myCat.name="Luna";
myCat.sayMeow();
myCat.lives=7;
return (`This is my cat, ${myCat.name} her fur is ${myCat.coatPattern} ${myCat.furColor} and she says ${myCat.sayMeow()} ${myCat.lives} times`);
}

console.log(myCat())

Properties

JavaScript provides static utilities/methods such as valueOf(), Object.hasOwn(), and Object.defineProperty() that can be used to work with objects.

If a static method is not readily available, calling the Object.prototype directly is possible using the call() method.

class Cat{
furColor;
coatPattern;
name;

sayMeow(){
console.log(this.lives)
return "Meow"
}
}

function myCat(){
let myCat = new Cat();
myCat.furColor="black";
myCat.coatPattern="solid";
myCat.name="Luna";
myCat.sayMeow();
myCat.lives=7;
return (`This is my cat, ${myCat.name} her fur is ${myCat.coatPattern} ${myCat.furColor} and she says ${myCat.sayMeow()}`);
}


console.log(Object.hasOwn(myCat, 'name'))
console.log(Object.valueOf(myCat()))
myCat.call(Cat.sayMeow)

Additional Static Object Methods

1) Object.assign()

This static method copies all the content of the source object into a target object. The target object properties are overwritten by those properties in the source object if the property key is the same in both.

This method uses Get on the source and Set on the target and in so doing, assigns properties.

Get Zainab Jalloh’s stories in your inbox

Join Medium for free to get updates from this writer.

The output for console.log(newCat) is the return value for the myCat() function. You can have multiple source objects.

//source Object
function myCat(){
let myCat = new Cat();
myCat.furColor="black";
myCat.coatPattern="solid";
myCat.name="Luna";
myCat.sayMeow();
myCat.lives=7;
return (
`This is my cat, ${myCat.name} her fur is ${myCat.coatPattern} ${myCat.furColor} and she says ${myCat.sayMeow()}`
//{myCat: `This is my cat, ${myCat.name} her fur is ${myCat.coatPattern} ${myCat.furColor} and she says ${myCat.sayMeow()}`}

);
}

//target object
const newCat = Object.assign({}, [myCat()]);
console.log(newCat)

This option returns the string value “This is my cat, Luna her fur is solid black and she says Meow.” Specify the source as an array to ensure the string is returned and not the individual characters.

//source Object
function myCat(){
let myCat = new Cat();
myCat.furColor="black";
myCat.coatPattern="solid";
myCat.name="Luna";
myCat.sayMeow();
myCat.lives=7;
return (
{myCat: `This is my cat, ${myCat.name} her fur is ${myCat.coatPattern} ${myCat.furColor} and she says ${myCat.sayMeow()}`}

);
}

//target object
const newCat = Object.assign({}, myCat());
console.log(newCat)

This returns a key-value pair, myCat: “This is my cat, Luna, her fur is solid black and she says Meow.”

//first source Object
function myCat(){
let myCat = new Cat();
myCat.furColor="black";
myCat.coatPattern="solid";
myCat.name="Luna";
myCat.sayMeow();
myCat.lives=7;
return (
myCat
);

}

//second source object
function newCat(){
let newCat = new Cat();
newCat.furColor="white";
newCat.coatPattern="solid"
newCat.name="Artemist";

return (newCat)

}

//target object
let myCats = new Cat();
myCats = Object.assign(myCats, [myCat(), newCat()]);
console.log(myCats)

2) Object.create()

This static method copies an existing object and uses it as a prototype to create a new object.

The properties and their values from the prototype object are inherited by the new object. If enabled, these properties can be overwritten by the new object is the new object defines a new value for the property.

let pet = Object.create(myCat(), {
name:{
value: "Tabby",
enumerable: true,
writable: true
},
personality: {
value: function(){
return `${this.name} is cute`
}
}
})

console.log(pet.name)
console.log(`My new cat ${pet.name} has ${pet.coatPattern} ${pet.furColor} fur and ${pet.personality()} when she says ${pet.sayMeow()}`)

In the example above, we overwrite the name property from the myCat() prototype.

3) Object.entries()

This static method returns an array of the key-value pairing of an objects properties.

This is similar to using the for…in iterative loop.

for(const [key, value] of Object.entries(pet)){
console.log(`${key}: ${value}`)
}

const map = new Map(Object.entries(pet))
console.log(map)

Alternatively, you can use the Object.keys() and Object.values() static methods to extract only the keys or values of an object.

For additional information about JavaScript object and object methods, refer to the JavaScript documentation on Objects.

--

--

Zainab Jalloh
Zainab Jalloh

Written by Zainab Jalloh

Privacy Advocate. Humanitarian.