2

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

hfbachri_
  • 107
  • 1
  • 11
  • 1
    Possible duplicate of [Function overloading in Javascript - Best practices](https://stackoverflow.com/questions/456177/function-overloading-in-javascript-best-practices) – Hyyan Abo Fakher May 11 '18 at 11:43
  • Yes, leaving attributes null or undefined won't be a problem unless your code tries using that value – Chirag Ravindra May 11 '18 at 12:02

1 Answers1

1

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?