I have many objects for example lets say one of the object name is flipkart
let flipkart = function() {
this.name = 'flipkart'
// other functionality....
}
flipkart.prototype.someFunction = function(someValue) {
console.log(someValue);
};
this is what I am trying to do I get the object names in variable lets say
let myVariable = 'flipkart';
I want to create a new object or access flipkart object which is already created.
const objFlipkart = Object.create(myVariable);
objFlipkart.someFunction('someValue');
Is there any way to get this working.
If you look on MDN docs, you can see something about Object.create() where you can insert an object like:
const flipkart = {
name: 'flipkart',
someFunction: function(someValue) {
console.log(someValue);
}
}
and then make a copy of said flipkart by using Object.create():
const copiedFlipkart = Object.create(flipkart);
where you will then have the functionality with the new (exact same, but stored in a new memory location, so any changes won't affect the original flipkart) you wanted:
copiedFlipkart.name = "A new flipcart";
copiedFlipkart.someFunction('someValue');