使用“this =”的Javascript函数给出“赋值中的左侧无效” [英] Javascript function using "this = " gives "Invalid left-hand side in assignment"

查看:1275
本文介绍了使用“this =”的Javascript函数给出“赋值中的左侧无效”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个JavaScript对象使用另一个对象的构造函数的this赋值,并假设所有对象的原型函数。以下是我正在尝试完成的示例:

I am trying to get a JavaScript object to use the "this" assignments of another objects' constructor, as well as assume all that objects' prototype functions. Here's an example of what I'm attempting to accomplish:

 /* The base - contains assignments to 'this', and prototype functions
  */
 function ObjX(a,b) {
     this.$a = a;
     this.$b = b;
 }

 ObjX.prototype.getB() {
     return this.$b;
 }

 function ObjY(a,b,c) {
    // here's what I'm thinking should work:
    this = ObjX(a, b * 12); 
    /* and by 'work' I mean ObjY should have the following properties:
     * ObjY.$a == a, ObjY.$b == b * 12,
     * and ObjY.getB == ObjX.prototype.getB
     * ... unfortunately I get the error: 
     *     Uncaught ReferenceError: Invalid left-hand side in assignment
     */

    this.$c = c; // just to further distinguish ObjY from ObjX.
 }

我很感激你对如何让ObjY包含ObjX的作业的看法到'this'(即不必重复ObjY构造函数中的所有 this。$ * = * 赋值)并让ObjY假设ObjX.prototype。

I'd be grateful for your thoughts on how to have ObjY subsume ObjX's assignments to 'this' (i.e. not have to repeat all the this.$* = * assignments in ObjY's constructor) and have ObjY assume ObjX.prototype.

我的第一个想法是尝试以下方法:

My first thought is to try the following:

function ObjY(a,b,c) {
   this.prototype = new ObjX(a,b*12);
}

理想情况下,我想学习如何以原型方式做到这一点(即不必使用任何经典OOP替代品,如 Base2 )。

Ideally I'd like to learn how to do this in a prototypal way (i.e. not have to use any of those 'classic' OOP substitutes like Base2).

值得注意的是ObjY将是匿名的(例如 factory ['ObjX'] = function(a,b,c){this = ObjX( a,b * 12); ...} ) - 如果我的术语是正确的。

It may be noteworthy that ObjY will be anonymous (e.g. factory['ObjX'] = function(a,b,c) { this = ObjX(a,b*12); ... }) -- if I've the terminology right.

谢谢。

推荐答案

你不能真的这样做,因为 这个 值的定义是 immutable ,你无法改变以这种方式引用它。

You can't really do that, because the this value by definition is immutable, you can't change in that way what it references to.

解决方法是使用 call 应用 方法来运行 ObjX 对象中的构造函数 ObjY

A workaround would be to use the call or apply methods to run your ObjX constructor function in the this object of ObjY:

function ObjY(a,b,c) {
  ObjX.call(this, a, b * 12); 
  this.$c = c;
}

在上面的例子中, ObjX 执行函数更改其值,因此您在此函数中对该对象所做的所有属性扩展都将反映在<$ c的新对象中$ c>此值在 ObjY 构造函数中引用。

In the above example, the ObjX function is executed changing its this value, so all property extensions you make to that object in this function, will be reflected in the new object that the this value refers in the ObjY constructor.

一旦调用方法结束,此对象将被扩充,您可以制作更多属性扩展,例如添加 $ c value。

As soon the call method ends, the this object will be augmented and you can make more property extensions, like adding your $c value.

编辑:关于原型,您的样本将无效,因为 prototype property对于stances中的对象没有特殊含义,它将与任何其他属性一样,它应该用在构造函数上。

About the prototypes, your sample will not work, because the prototype property has no special meaning for object in stances it will be just as any other property, it should be used on constructor functions.

我认为你可能会混淆构造函数的 prototype 属性与内部 [[Prototype]] 所有对象都具有的属性。

I think you might be confusing the prototype property of constructors with the internal [[Prototype]] property that all objects have.

[[Pro totype]] 属性只能由 new 运算符设置(通过 [[Construct]] 内部操作),此属性无法更改,(虽然有些实现,如Mozilla,您可以通过 obj .__ proto __; 访问它,并在 ECMAScript 5 Object.getPrototypeOf 方法已经推出,但我不建议你直接搞乱它。)

The [[Prototype]] property can only be set by the new operator (through the [[Construct]] internal operation), this property can't be changed, (although some implementations, like the Mozilla one, you can access it through obj.__proto__;, and in ECMAScript 5, the Object.getPrototypeOf method has been introduced, but I wouldn't recommend you to mess directly with it).

实际上,当执行构造函数时,<$ c对象的内部 [[Prototype]] 属性$ c>这个值引用,已被设置为其构造函数的 prototype 属性。

Actually, when your constructor function is executed, the internal [[Prototype]] property of the object that the this value refers, has already been set to its constructor's prototype property.

所以,正如@Anurag评论,您可以将 ObjY.prototype 设置为新创建的 ObjX 对象:

So, as @Anurag comments, you could set the ObjY.prototype to a newly created ObjX object:

function ObjY(a,b,c) {
  ObjX.call(this, a, b * 12); 
  this.$c = c;
}

ObjY.prototype = new ObjX();
ObjY.prototype.constructor = ObjY;

这将使您的 ObjY 继承添加到 ObjX.prototype 的属性,如您所见,我更改了 ObjY.prototype.constructor ,因为上面一行中的赋值将使此属性错误地指向 ObjX

That will make that your ObjY inherit also the properties added to the ObjX.prototype, and as you see, I changed the ObjY.prototype.constructor, since the assignment in the line above will make this property to wrongly point to ObjX.

推荐文章:

  • Constructors considered mildly confusing
  • JavaScript Prototypal Inheritance

这篇关于使用“this =”的Javascript函数给出“赋值中的左侧无效”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆