[英]Why Function.prototype cannot be modified ?
in the Function.prototype page it's written this : 在Function.prototype页面中这样写:
Function objects inherit from Function.prototype.
函数对象继承自Function.prototype。 Function.prototype cannot be modified.
Function.prototype无法修改。
Or in javascript there are no classes but the Inheritance and the prototype chaining in which constructors are actually functions : 或者在javascript中,除了继承和原型链(其中构造函数实际上是函数)之外,没有任何类:
function AclassName(){
return 2;
}
// AclassName ---> Function.prototype ---> Object.prototype ---> null
and i think it's always possible to extend the class prototype's like : 而且我认为始终可以扩展类原型,例如:
AclassName.prototype.color = "somevlue";
So what does it mean that i can't be modified ? 那我不能被修改意味着什么?
Everything in JS has a prototype (even if it's null). JS中的所有内容都有一个原型(即使它为null)。 So, the prototype of an actual
function
is Function.prototype
. 因此,实际
function
的原型是Function.prototype
。
When you assign or modify AclassName.prototype
in your example, you're setting the prototype for instances of AclassName
. 在示例中分配或修改
AclassName.prototype
时,您正在设置AclassName
实例的AclassName
。 Note that the prototype of an object x
is not the same as x.prototype
. 请注意,对象
x
的原型与 x.prototype
。 That .prototype
is used for setting the prototype that will be used for instances of x, if x is used as a constructor. 如果将x用作构造函数,则
.prototype
用于设置将用于x 实例的原型。
To put it another way: 换一种方式:
Your function AClassName, declared with function AClassName () {}
, is an object of class Function , so it inherits from Function.prototype
. 使用
function AClassName () {}
声明的function AClassName () {}
是Function类的对象,因此它继承自Function.prototype
。
If you instantiate that class: 如果您实例化该类:
var myInstance = new AClassName();
Then myInstance
is an object of class AClassName , so it inherits from AClassName.prototype
. 然后
myInstance
是类AClassName的对象,因此它继承自AClassName.prototype
。
So to answer the root of your question: Function.prototype
cannot be modified, because it's a core part of the language and being able to change it might introduce performance or security issues. 因此,请回答您问题的根源:无法修改
Function.prototype
,因为它是语言的核心部分,并且能够对其进行更改可能会引入性能或安全性问题。 However, you are totally at liberty to modify the prototypes of your own classes. 但是,您完全可以自由修改自己的类的原型。
I have to point it out that:the prototype of Function is Function.prototype
, but a specific function like foo, it's prototype is not Function.prototype
. 我必须指出:Function的原型是
Function.prototype
,但是像foo这样的特定函数,它的原型不是Function.prototype
。
Function.prototype
is a callable object, it is a "function" (typeof Function.prototype).foo.prototype
is an "object". Function.prototype
是可调用的对象,它是一个“函数” (typeof Function.prototype).foo.prototype
是一个“对象”。 When you Function constructor construct a function like foo, it runs a code: this.prototype={constructor:this}
(This is from "The JavaScript: good parts", Douglas Crockford); 当您的Function构造函数构造类似foo的函数时,它将运行代码:
this.prototype={constructor:this}
(来自“ JavaScript:好的部分”,Douglas Crockford); ie foo.prototype={constructor:foo}
. 即
foo.prototype={constructor:foo}
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.