1

I have model called Account_Model.js that contains query to create, read, update and delete (CRUD) user account. I need different constructor (to create and update, I need to pass username, fullname, password etc to the constructor, but when I want to delete user account, I only need to pass username).

var connection = require('../config/db.js');

class Account_Model{
    constructor(params){
      this.username = params.username,
      this.fullname = params.fullname,
      this.password = params.password
}
}
getData(){}....

Is this a good practice? Cause when I delete the user, I only pass username in Account_Model instance and left fullname and password null. Thank you

2

1 Answer 1

0

So ES6 does not allow you to have multiple constructors and to be able to overload methods.

Your way of passing an object and only populating certain fields is perfectly valid and is seen in many NPM modules.

You can also look at this example which shows a different way of handling multiple parameters being passed in by checking the length of the passed in parameters: Why doesn't JavaScript ES6 support multi-constructor classes?

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

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.