When i tried to create an instance of DOM's HTMLElement,
var oElement = new HTMLElement();
It throws : TypeError: Illegal constructor
Why can't we instantitate DOM's Constructor Functions ? Is there a way to do it? Thanks
In the above example, function Person() is an object constructor function. To create an object from a constructor function, we use the new keyword. Note: It is considered a good practice to capitalize the first letter of your constructor function.
Calling the constructor directly can create functions dynamically, but suffers from security and similar (but far less significant) performance issues as eval() . However, unlike eval (which may have access to the local scope), the Function constructor creates functions which execute in the global scope only.
Object.create(HTMLElement.prototype, {})
To create a new element with Javascript you use the createElement method of the document object.
var newDiv = document.createElement("div");
To add some new text with Javascript you can use the createTextNode method of the document object.
var text = document.createTextNode("Text goes here");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With