Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an instance of DOM's Constructor functions?

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

like image 806
ikiw Avatar asked Apr 18 '14 09:04

ikiw


People also ask

How do you create a function from a constructor?

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.

Can you put functions in constructors?

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.


2 Answers

Object.create(HTMLElement.prototype, {})
like image 192
Martin Wantke Avatar answered Nov 15 '22 20:11

Martin Wantke


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");
like image 45
Ruslan Ismagilov Avatar answered Nov 15 '22 20:11

Ruslan Ismagilov