1
I’m trying to create a personal library, where the original idea was to extend native javascript objects with various useful functions. After reading a little, I ended up convincing myself that extending native objects is not exactly a good idea, so I came up with the idea of creating new constructors exactly like the native constructors, and then adding the methods to them, so as not to affect the originals. The idea would be something like this:
var MyDate = Date;
MyDate.prototype.teste = function() {
console.log('teste');
};
var a = new MyDate();
var b = new Date();
It works well, only the problem is...
a.teste() // 'teste'
b.teste() // 'teste'
I understood that this occurs because in fact MyDate
became just a reference to Date
, and not a new constructor, but in this case, how can I "clone" a constructor completely?