JavaScript - 尝试从原型中添加原型

发布于 2024-11-01 16:05:04 字数 357 浏览 8 评论 0原文

这听起来比应有的容易,但我在尝试解决此问题时遇到了问题。我的情况基本上是,我可以创建一个将使用原型的类(例如:function exClass() {})。

如果我想添加到类中,我可以使用:exClass.prototype.newMethod() = '';

那么为什么如果我处于原型“newMethod”中,我就无法再向“exClass”添加新原型了。我的意思的一个例子是: this.prototype.test_var1 = '' - 它失败了,exClass.test_var1 也是如此。

为什么我无法从某个子类中向该类添加更多内容?

This sounds easier than it should be, but I'm having issues trying to resolve this. My situation is basically, I can create a class that's going to use prototype (example: function exClass() {}).

And if I want to add onto the class, I can just use: exClass.prototype.newMethod() = '';.

So why is it that if I'm in, for example, the prototype "newMethod", I can't add a new prototype to "exClass" anymore. An example of what I mean is: this.prototype.test_var1 = '' - it fails, as does exClass.test_var1.

Why am I unable to add more to the class from within one of its subclasses?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼前雾蒙蒙 2024-11-08 16:05:05

对象的原型不是对象上称为prototype的属性。函数上的原型字段是将成为使用该函数创建的对象的原型的对象。对象可以通过构造函数访问创建它的函数的原型。例如,this.constructor.prototype.test_var1 = '' 在大多数情况下都有效。

我之所以说在大多数情况下,是因为许多 JavaScript 引擎都有一个内置的 __proto__ ,它是对象的原型,可以动态修改,但例如 IE 不支持这一点。

在 ES5 中,您可以使用 Object.getPrototypeOf() 可靠地获取原型。例如,您可以在 ES5 中使用 Object.getPrototypeOf(this).test_var1 = '' ,它可以在现代浏览器中工作,但不能在不支持 ES5 的浏览器中工作。

The prototype of an object is not a property called prototype on the object. The prototype field on a function is the object that will become the prototype of objects created with that function. An object can access the prototype of the function that created it through the constructor function. For example, this.constructor.prototype.test_var1 = '' would work in most cases.

I say in most cases because many JavaScript engines have a built in __proto__ that is the object's prototype and can be modified on the fly, but this is not supported in IE for example.

In ES5 you can use Object.getPrototypeOf() to get the prototype reliably. So for example, you can say, Object.getPrototypeOf(this).test_var1 = '' in ES5 which will work in modern browsers but not browsers without ES5 support.

Hello爱情风 2024-11-08 16:05:05

构造函数 exClassprototype 属性与实例的 prototype 属性引用的对象不同exClass 的,这是 thisnewMethod 中引用的内容。证明:

function exClass() {}
exClass.prototype.newMethod = function() {
    console.log(this.prototype === exClass.prototype); // false
    console.log(this.prototype); // undefined
}

var obj = new exClass();
obj.newMethod();

输出:

false
undefined

更一般地,JavaScript 中的每个对象都有一个原型 object。函数的原型属性指定使用该函数创建的对象类的原型对象

没有什么可以阻止您从另一个函数中修改函数的原型:

exClass.prototype.newMethod = function() {
    exClass.prototype.anotherMethod = function() {}
}

或者更一般地说:

exClass.prototype.newMethod = function() {
    this.constructor.anotherMethod = function() {}
}

但我不推荐它。

The prototype property of the constructor function exClass does not refer to the same object as the prototype property of an instance of exClass, which is what this references inside newMethod. Proof:

function exClass() {}
exClass.prototype.newMethod = function() {
    console.log(this.prototype === exClass.prototype); // false
    console.log(this.prototype); // undefined
}

var obj = new exClass();
obj.newMethod();

Output:

false
undefined

More generally, every object in JavaScript has a prototype object. The prototype property of a function specifies what the prototype object will be for the class of objects created with that function.

Nothing prevents you from modifying a function's prototype from within another function:

exClass.prototype.newMethod = function() {
    exClass.prototype.anotherMethod = function() {}
}

Or more generically:

exClass.prototype.newMethod = function() {
    this.constructor.anotherMethod = function() {}
}

But I wouldn't recommend it.

祁梦 2024-11-08 16:05:05

您无法通过 this.prototype 获取对象的父原型。您必须使用 this.constructor.prototype (尽管这会影响该类型的所有对象的行为,在本例中是 exClass)。该片段将提醒“hello world”。

function exClass() {};

exClass.prototype.newMethod = function() {
    this.constructor.prototype.test_var1 = 'hello world';
}

obj = new exClass();
obj.newMethod();
alert(obj.test_var1);

You can't get at an object's parent prototype via this.prototype. You have to use this.constructor.prototype (although this will affect the behavior of all objects of that type, in this case exClass). This snippet will alert 'hello world'.

function exClass() {};

exClass.prototype.newMethod = function() {
    this.constructor.prototype.test_var1 = 'hello world';
}

obj = new exClass();
obj.newMethod();
alert(obj.test_var1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文