简体   繁体   中英

Creating own Document Javascript

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM