I would like to be able to create a class that looks like the object that was passed to it, and add new methods to this object.
Here is what i have so far:
Node = function(data) {
Node = data;
Node.constructor.prototype = new data.constructor();
Node.constructor.prototype.testProp = "Hello!";
return Node;
};
node = Node('abc');
console.log(node); // abc
console.log(node.testProp); // Hello!
var testArray = [];
testArray.push(node);
console.log(testArray); // ['abc']
Whats the problem with this implementation?
The Node class looks like a String in this sample but every string now has a testProp property.
console.log(node.testProp) // 'Hello!'
console.log("something".testProp) // 'Hello!'
My question:
How i should implement a class that would behave like the object that was passed in the constructor without affecting all the other objects of the same class?
Why?
The reason why i am asking this is that i want the element data (String, Number, Array, Object, etc) to be accessible without using any methods or props for example console.log(Node.value);
, instead i just want to use console.log(Node);
Thanks!