I have an Object which I am getting from API response. Now I want to perform some operation on that object and I want to do it in prototype way.
let's say I am getting following object
const user = {
name: "Carlos",
gender: "male"
}
Now I want to create a prototype function which can run directly on object like. user.isMale()
how can I achieve the same in ES6
isMalemethod to every object? It would make more sense to create aUserclass.Userclass can be done, this resource can be useful: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…JSON.parse(json), then you can eithercarlos = Object.assign(new User(), JSON.parse(json))or you canObject.setPrototypeOf(carlos = JSON.parse(json), User.prototype). See this answer