As I am jumping into Javascript a little deeper, I got this odd result when trying a test.
function CustomeObject() {
this.type = "custom";
};
var node1 = document.createTextNode(Date.prototype);
var node2 = document.createTextNode(CustomeObject.prototype);
document.getElementsByTagName("body")[0].appendChild(node1);
document.getElementsByTagName("body")[0].appendChild(node2);
And the result is as follow:
Invalid Date [object Object]
As I read from one source on the internet and it says: the prototype is a buil-in property of any object, and it’s actually an object itself. But this test failed with the Date Object.
Could you tell me what wrong with my code to test Date prototype property?
Thank you!
When you pass
Date.prototypetodocument.createTextNode()it will implicitly calltoString()on the passed object.The default output of
toString()is[object Object], as seen in your second test.However
Date.prototypehas its owntoString()function whose purpose is to return the currentDateobject (i.e.this) as text.When you call that function directly its
thispointer incorrectly containsData.prototypeinstead of a date object, hence the"Invalid Date"output.