0

So I am trying to use the Document() constructor method to create my own document but I fail at Illegal Invocation Error. Could someone explain this behavior?

mydom = new Document()
// TypeError: Illegal constructor
var MyDom = new Function()
// undefined
MyDom.prototype
// Object {}
MyDom.prototype = Document.prototype
// Document {createElement: function, createDocumentFragment: function, createTextNode:      function, createComment: function, createCDATASection: function…}
myowndom = new MyDom()
// Document {createElement: function, createDocumentFragment: function, createTextNode:    function, createComment: function, createCDATASection: function…}
myowndom.createElement('h1')
// TypeError: Illegal invocation
Document.prototype.constructor
// function Document() { [native code] }
myowndom.createAttribute.call(Document, "h1")
// TypeError: Illegal invocation
sudo
  • 343
  • 1
  • 11
  • 1
    Possible duplicate http://stackoverflow.com/questions/8227612/how-to-create-document-objects-with-javascript – leorex Dec 15 '13 at 22:33
  • I've seen that question. My question is different because it's focused on the prototype chain. – sudo Dec 15 '13 at 22:40
  • You are not suppose to call the Document function. Check the latest answer, pretty much the same as my comment. – leorex Dec 15 '13 at 22:41
  • So Google has created Chrome with an host object Document. That they restricted to call, for what reason? – sudo Dec 15 '13 at 22:42
  • @user3063183: They gave you host objects like `console` or `Element` that you cannot call either. Why would you want to? – Bergi Dec 15 '13 at 22:52
  • @user3063183 looking at the standard, I think you should be able to call it, but DOM4 is still work in progress http://www.w3.org/TR/dom/#document – leorex Dec 15 '13 at 22:54
  • @Bergi I don't like restrictions in any programming language if they are not very crucial to exist. If every browser will create thier own host object with thier own rules then the whole language will become inconsistent and you will need huge libraries to straight the language up so it could work for all browsers. If the constructors are meant to create new Objects from, then just let folks do it why not. Explain prototype to someone that is learning Javascript, they will come against such Document constructor and it will confuse the hell out of that person. – sudo Dec 15 '13 at 23:20
  • @user3063183: Yeah, they're confusing indeed, and there are many flaws in the language that cannot easily be changed without breaking backwards-compatibility. At least, the DOM is standardized and *no browser* allows creating DOM elements from their constructors (the exceptions being `Image` and `Option` :-)) – Bergi Dec 16 '13 at 00:20

1 Answers1

0

The Document function is not intended to be called, but is there only as a convenience variable. Documents are host objects (with a very complex underlying API) and are not easily constructed.

If you want to create an extra Document instance, you can use the document.implementation.createDocument() method for that.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • It's there so I should be able to call it. Why is it there if you can't do anything with it anyway? – sudo Dec 15 '13 at 22:44
  • Check the linked question to see what you can do with it. Most prominently, it's a holder for its `.prototype` property. – Bergi Dec 15 '13 at 22:49