创建自己的文档Javascript



所以我试图使用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

Document函数不是用来调用的,只是作为一个方便变量。文档是宿主对象(具有非常复杂的API底层),不容易构建。

如果要创建一个额外的Document实例,可以使用document.implementation.createDocument()方法。

最新更新