I'd like to create a new object working like Date
or Array
in JavaScript, which can be called like a function Date()
. Meanwhile, there are also other functions on it like Date.now()
or Array.of()
, which returns a new instance of this object.
How can I do that?
For example:
function Foo (lastName) {
this.firstName = 'Foo'
return this.firstName + ' ' + lastName
}
Foo('Bar') // 'Foo Bar'
Foo.setFirstName = function (firstName) {
this.firstName = firstName
/**
* Here I want to return a new instance of Foo, with new firstName
*/
}
const foo = Foo.setFirstName('foo')
foo('bar') // 'foo bar'