0

I'm new to javascript and I have a question: I'm trying to extend a function that takes a parameter and that same parameter is present in the methods of the object it returns. However, I would like to know if it's possible to have a second function that returns an object that extends the first function and those parameters are accessible in the second function.

const provider = (firstName) => (
{ 
  lastName: "Doe",
  id: 5566,
  method: function() {
    return firstName + " " + this.lastName;
  }
});

const extender = (prov, )=> ({
    ...prov,
    method_2: function (){
        return firstName + " " + this.lastName;
    }
    
});



console.log(provider("John").method())

ext_inst = extender(provider("John"))
console.log(ext_inst.method())
//console.log(ext_inst.method_2())
When I run method 2 of the code above, I get an error. It's ok, I was waiting for this error! But would there be a way to get around this and get the parameters of the function I'm extending?
iaggo
  • 51
  • 5
  • 1
    No halfway decent way. You would have to change the `provider` to allow the `firstName` to be accessible elsewhere, like by putting a property onto the returned object. – CertainPerformance Nov 20 '22 at 22:15
  • 1
    Perhaps you are asking about this: https://marmelab.com/react-admin/DataProviders.html#handling-file-uploads – MaxAlex Nov 21 '22 at 02:52

0 Answers0