如何扩展文档对象?

Nop*_*ope 6 javascript prototype

我目前正在努力更好地理解JavaScript和原型设计.

我想添加一个函数documentprototype未定义document.

这段代码:

document.prototype.writeLine = function(text){
    this.write(text);
    this.write("<br />");  
};
Run Code Online (Sandbox Code Playgroud)

生成此错误:

// In FireFox
TypeError: document.prototype is undefined

// In Chrome
Uncaught TypeError: Cannot set property 'writeLine' of undefined 
Run Code Online (Sandbox Code Playgroud)

如何扩展document对象以便能够调用类似的东西document.WriteLine('MyText')

这是我正在使用的小提琴.

Kon*_*nev 8

我更新了你的小提琴.您遇到的问题是该document对象是HTMLDocument对象类型的实例.该实例本身不具有原型,但是HTMLDocument确实.

更新:这是一个小提琴更新,适用于IE9,因为在IE9 HTMLDocumentundefined.