创建自己的文档Javascript

时间:2013-12-15 22:27:45

标签: javascript prototype

所以我尝试使用Document()构造函数方法创建自己的文档,但是我在Illegal Invocation Error失败了。有人可以解释这种行为吗?

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

1 个答案:

答案 0 :(得分:0)

Document function is not intended to be called,但仅作为便利变量。文档是宿主对象(具有非常复杂的底层API)并且不易构造。

如果您想创建额外的Document个实例,可以使用document.implementation.createDocument() method