2013-12-15 36 views
0

所以我試圖使用Document()構造函數方法來創建我自己的文檔,但我在Illegal Invocation Error失敗。有人可以解釋這種行爲嗎?創建自己的文檔Javascript

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

可能的重複http://stackoverflow.com/questions/8227612/how-to-create-document-objects-with-javascript – leorex

+0

我見過這個問題。我的問題不同,因爲它專注於原型鏈。 – sudo

+0

您不打算調用Document函數。檢查最新的答案,幾乎與我的評論相同。 – leorex

回答

0

Document function is not intended to be called,但是隻有作爲一個便利的變量。文檔是宿主對象(具有非常複雜的底層API),並且不容易構建。

如果您想創建一個額外的Document實例,那麼可以使用document.implementation.createDocument() method

+0

它就在那裏,所以我應該可以調用它。無論如何,如果你無法做任何事情,爲什麼還有呢? – sudo

+0

檢查鏈接的問題,看看你可以用它做什麼。最顯着的是,它是'.prototype'屬性的持有者。 – Bergi