1

I want to know how i can expand function and add more arguments for it like object. For example I have function

function xml(){}

xml.prototype.get = function(url, config /*optional*/){
    //something with restApi
}

and i want to use mock data for every method and i try to add mock and mockData like new parameters in object but if i don't pass config argument all brokes, but i want to use it outside object.

const httpClient = Object.create(xml)
httpClient.get = function(url, config /*optional*/,{mock,mockData}={}){
        if(mock){
          return new Promise(resolve=>resolve({data: mockData}))
        }
        return this.get(url, config)
    }
1
  • you can use like this {mock:undefined,mockData:undefined} or you can pass object variable function(url, config /*optional*/,MyObject={}) Commented Aug 29, 2019 at 10:54

2 Answers 2

2

You can simply have all of the arguments as object property

function dummmy({mock,mockData,url,config}={}){
   if(mock){
      return new Promise(resolve=>resolve({data: mockData}))
    }
  return this.get(url, config)
}

And then pass whatever values you want to pass

dummy({url:'someurl', mock:[1,2,3]})

function dummy( {a,b} = {} ){
  console.log(a,b)
}

dummy({a:1})
dummy({a:1, b:2})

Sign up to request clarification or add additional context in comments.

Comments

0
const httpClient = Object.create(xml)
httpClient.get = function(url, config, mock, mockData) {
    if (!!mock && !mockData) {
        mockData = mock;
        mock = config;
        config = null;
    }
    if(mock){
      return new Promise(resolve=>resolve({data: mockData}))
    }
    return this.get(url, config)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.