javascript Class.prototype.method 与 this.prototype.method
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22123965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Class.prototype.method vs this.prototype.method
提问by 00500005
I've always seen examples of Class.prototype.method, but never instances of this.prototype.method. For example:
我总是看到 Class.prototype.method 的例子,但从未见过 this.prototype.method 的实例。例如:
function Class() {
this.prototype.method = function() {
alert("is this allowed?");
};
}
vs
对比
function Class() {}
Class.prototype.method = function() {
alert("traditional example");
};
- Does this.prototype exist?
- Is it the same as Class.prototype?
- What about inheritance?
- this.prototype 存在吗?
- 和 Class.prototype 一样吗?
- 继承呢?
回答by Jonathan Lonowski
Does this.prototype exist?
No. But,
this.constructor.prototype
should.Is it the same as Class(.constructor).prototype?
It will generally have the same value (so long as
this.constructor === Class
), but not the same intent.What about inheritance?
Each
new Class
instance will inherit fromClass.prototype
. So, aprototype
object is good for defining shared values accessible to all instances. Then, the constructor just has to setup state that's unique to each instance.But, attempting to mix the 2 and set a
prototype
property within the constructor has a few problems, including a chicken-or-egg conflict as the method won't exist until the first instance is created:function Class() { this.constructor.prototype.method = function () {}; } console.log(typeof Class.prototype.method); // "undefined" var a = new Class(); console.log(typeof Class.prototype.method); // "function"
And defeats some of the benefit of having the
prototype
as the method is being recreated with each additional instance:var method = a.method; console.log(a.method === method); // true var b = new Class(); console.log(a.method === method); // false
this.prototype 存在吗?
this.constructor.prototype
不。但是,应该。和 Class(.constructor).prototype 一样吗?
它通常具有相同的值(只要
this.constructor === Class
),但意图不同。继承呢?
每个
new Class
实例都将从Class.prototype
. 因此,prototype
对象适用于定义所有实例都可以访问的共享值。然后,构造函数只需要设置每个实例唯一的状态。但是,尝试混合 2 并
prototype
在构造函数中设置属性有一些问题,包括先有鸡还是先有蛋的冲突,因为在创建第一个实例之前该方法不存在:function Class() { this.constructor.prototype.method = function () {}; } console.log(typeof Class.prototype.method); // "undefined" var a = new Class(); console.log(typeof Class.prototype.method); // "function"
并且失去了一些好处,
prototype
因为每个额外的实例都会重新创建方法:var method = a.method; console.log(a.method === method); // true var b = new Class(); console.log(a.method === method); // false
回答by VtoCorleone
this.prototype would refer to that instance of the class object. It wouldn't give you much benefit because of the scope.
this.prototype 将引用类对象的实例。由于范围的原因,它不会给您带来太多好处。
Class.prototype is adding functionality to the Class, not the instance of it.
Class.prototype 正在向类添加功能,而不是它的实例。
回答by user2310967
Class
is a function; this
is an object. Functions have a prototype
property; objects do not. There's a __proto__
property defined on objects, but that interface is deprecated. You can do something like
Class
是一个函数;this
是一个对象。函数有一个prototype
属性;对象没有。__proto__
在对象上定义了一个属性,但不推荐使用该接口。你可以做类似的事情
function Class () {
var prototype = Object.getPrototypeOf(this);
prototype.method = function () {};
}
inside your constructor but it's not really good practice - every time Class
is instantiated, it will needlessly waste cycles overwriting method
on the prototype, and in cases with more complex code, may end up wasting memory as well.
在您的构造函数内部,但这并不是一个很好的做法 - 每次Class
实例化时,它都会不必要地浪费覆盖method
原型的周期,并且在代码更复杂的情况下,最终也可能会浪费内存。
In short, there's no upside and possibly serious downsides to doing it that way.
简而言之,这样做没有好处,也可能有严重的坏处。