I'm currently trying to get a better understanding of JavaScript and prototyping.
我目前正在努力更好地理解JavaScript和原型设计。
I wanted to add a function to the document
but prototype
is undefined on document
.
我想在文档中添加一个函数,但原型未在文档中定义。
This code:
document.prototype.writeLine = function(text){
this.write(text);
this.write("<br />");
};
Generates this error:
生成此错误:
// In FireFox
TypeError: document.prototype is undefined
// In Chrome
Uncaught TypeError: Cannot set property 'writeLine' of undefined
How can I extend the document
object to be able to call something similar to document.WriteLine('MyText')
?
如何扩展文档对象以便能够调用类似于document.WriteLine('MyText')的内容?
Here is the Fiddle I'm working with.
这是我正在与之合作的小提琴。
4 个解决方案
#1
8
I updated your fiddle. The problem you were having is that document
object is an instance of the HTMLDocument
object type. The instance itself doesn't have a prototype, however the HTMLDocument
does.
我更新了你的小提琴。您遇到的问题是文档对象是HTMLDocument对象类型的实例。实例本身没有原型,但HTMLDocument确实如此。
Update: Here is a fiddle update which works in IE9 because under IE9 HTMLDocument
is undefined
.
更新:这是一个小提琴更新,适用于IE9,因为在IE9下HTMLDocument未定义。
#2
1
The problem is that document
is of type object
and not function
. In JavaScript you use functions as constructors like this:
问题是文档是对象类型而不是函数。在JavaScript中,您使用函数作为构造函数,如下所示:
function MyClass() {
this.myProperty = "something";
}
You may create an instance of MyClass
as follows:
您可以按如下方式创建MyClass的实例:
var myInstance = new MyClass;
alert(myInstance.myProperty);
Every function also has a property called prototype
which is an object. All the properties of the prototype are inherited my instances of the constructor function:
每个函数都有一个名为prototype的属性,它是一个对象。原型的所有属性都继承了构造函数的实例:
MyClass.prototype.displayProperty = function () {
alert(this.myProperty);
};
myInstance.displayProperty();
In your case since document
is the instance of a constructor and not the constructor itself, there's no property called prototype
on it.
在你的情况下,因为document是构造函数的实例而不是构造函数本身,所以没有名为prototype的属性。
For more information about inheritance in JavaScript read this answer.
有关JavaScript中继承的更多信息,请阅读此答案。
#3
0
writeLine("Line 1");
writeLine("Line 2");
function writeLine(text){
document.write(text);
document.write("<br />");
};
#4
0
is very easy, document and Document are both different, document is the document of the window and Document is the interface of the document (thats comone from DOM), if your like add a new prototype for you use in your document you need add this but into Document like this:
window.Document.prototype.Sayhi = "Hello World" or Document.prototype.Sayhi = "Hello World" and now you can call this from you document like document.sayhi thats happen because you need Set the prototype on Interfaces if you like for example add a new prototype in your Object window your need Set it at Window interface like: Window.prototype.Saybye = "Bye Bro See You Later" and you can call the prototype in you window.Saybye remember, Window is an interface that contain window like Document and document****
非常简单,文档和文档都是不同的,文档是窗口的文档,文档是文档的接口(即DOM的comone),如果你喜欢在文档中添加一个新的原型,你需要添加这个但是像这样的文档:window.Document.prototype.Sayhi =“Hello World”或Document.prototype.Sayhi =“Hello World”,现在你可以从你的文件中调用这个文件,例如document.sayhi那是因为你需要设置原型在接口上,如果你喜欢在你的对象窗口中添加一个新的原型你需要在Window界面设置它,如:Window.prototype.Saybye =“再看见你以后”,你可以在你的窗口中调用原型。再见, Window是一个包含文档和文档****的窗口的界面
#1
8
I updated your fiddle. The problem you were having is that document
object is an instance of the HTMLDocument
object type. The instance itself doesn't have a prototype, however the HTMLDocument
does.
我更新了你的小提琴。您遇到的问题是文档对象是HTMLDocument对象类型的实例。实例本身没有原型,但HTMLDocument确实如此。
Update: Here is a fiddle update which works in IE9 because under IE9 HTMLDocument
is undefined
.
更新:这是一个小提琴更新,适用于IE9,因为在IE9下HTMLDocument未定义。
#2
1
The problem is that document
is of type object
and not function
. In JavaScript you use functions as constructors like this:
问题是文档是对象类型而不是函数。在JavaScript中,您使用函数作为构造函数,如下所示:
function MyClass() {
this.myProperty = "something";
}
You may create an instance of MyClass
as follows:
您可以按如下方式创建MyClass的实例:
var myInstance = new MyClass;
alert(myInstance.myProperty);
Every function also has a property called prototype
which is an object. All the properties of the prototype are inherited my instances of the constructor function:
每个函数都有一个名为prototype的属性,它是一个对象。原型的所有属性都继承了构造函数的实例:
MyClass.prototype.displayProperty = function () {
alert(this.myProperty);
};
myInstance.displayProperty();
In your case since document
is the instance of a constructor and not the constructor itself, there's no property called prototype
on it.
在你的情况下,因为document是构造函数的实例而不是构造函数本身,所以没有名为prototype的属性。
For more information about inheritance in JavaScript read this answer.
有关JavaScript中继承的更多信息,请阅读此答案。
#3
0
writeLine("Line 1");
writeLine("Line 2");
function writeLine(text){
document.write(text);
document.write("<br />");
};
#4
0
is very easy, document and Document are both different, document is the document of the window and Document is the interface of the document (thats comone from DOM), if your like add a new prototype for you use in your document you need add this but into Document like this:
window.Document.prototype.Sayhi = "Hello World" or Document.prototype.Sayhi = "Hello World" and now you can call this from you document like document.sayhi thats happen because you need Set the prototype on Interfaces if you like for example add a new prototype in your Object window your need Set it at Window interface like: Window.prototype.Saybye = "Bye Bro See You Later" and you can call the prototype in you window.Saybye remember, Window is an interface that contain window like Document and document****
非常简单,文档和文档都是不同的,文档是窗口的文档,文档是文档的接口(即DOM的comone),如果你喜欢在文档中添加一个新的原型,你需要添加这个但是像这样的文档:window.Document.prototype.Sayhi =“Hello World”或Document.prototype.Sayhi =“Hello World”,现在你可以从你的文件中调用这个文件,例如document.sayhi那是因为你需要设置原型在接口上,如果你喜欢在你的对象窗口中添加一个新的原型你需要在Window界面设置它,如:Window.prototype.Saybye =“再看见你以后”,你可以在你的窗口中调用原型。再见, Window是一个包含文档和文档****的窗口的界面