Question
So basically I'm trying to make a class with multi constructors so that it is more user friendly. but when I run the code it outputs this:
SyntaxError: /FileSystem.js: Duplicate constructor in the same class (13:2)
I know that Creating multiple constructor in ES6 kind of answers this question but my code has different parameters for each constructor:
Varun Sukheja's code
function Book() {
//just creates an empty book.
}
function Book(title, length, author) {
this.title = title;
this.Length = length;
this.author = author;
}
My Code
class File
constructor(Name, Type, Data) {
this.Name = Name
this.Type = Type
this.Data = Data
}
constructor(FileName, Data) {
let FileNameSplit = FileName.split('.').pop();
this.Type = FileNameSplit[FileNameSplit.length - 1];
let NameSplit = FileNameSplit.pop()
this.Name = FileName;
this.Data = Data
}
}
As you can see, my code has 2 constructors (one with constructor(Name, Type, Data) {
and the other with constructor(FileName, Data) {
). so you can see using Saransh Kataria's Code, won't work.
.
Saransh Kataria's Code
constructor(title, length, author) {
if(!arguments.length) {
// empty book
}
else {
this.title = title;
this.Length = length;
this.author = author;
}
}
Extra Info
IDE: Codesandbox
Browser: Chrome
Full Code:
class File {
/**
*
* @param {String} Name Name Of File
* @param {(String|Number)} Type File Type / Exstention
* @param {Array} Data
*/
constructor(Name, Type, Data) {
this.Name = Name
this.Type = Type
this.Data = Data
}
constructor(FileName, Data) {
let FileNameSplit = FileName.split('.').pop();
this.Type = FileNameSplit[FileNameSplit.length - 1];
let NameSplit = FileNameSplit.pop()
this.Name = FileName;
this.Data = Data
}
}
let Blob1 = new Blob([""])
console.log(Blob1)