且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JS类没有对“this”的常量引用。成员访问

更新时间:2023-01-17 17:44:40

在这里添加我自己的答案以供后代使用,因为在某个阶段,如果我被卡在之间***(1)前面和(2)使用可怕的,可能有一个(3)rd解决方案。



让我们假设我们不直接访问任何成员数据;



在这种情况下,可以使用Doug Crockford讨论的私有概念:

  MyClass = function()
{
var prop = undefined;
this.getProp(){return prop;}
this.setProp(value){prop = value;}

//现在在标准,非getter和-setter方法,我们可以做...
this.foo = function()
{
return prop + prop; // }
}

和第(4)个解决方案,不太安全,但也不太详细:

  MyClass = function {
var prop = this.prop = undefined; //this.prop为只读! (unsafe)
this.setProp(value){prop = this.prop = value;}

//现在在标准,非getter和-setter方法, 。
this.foo = function()
{
return prop + prop; // }
}

在(4)中,我们只需要setter,大大减少了冗余。



使用几个包含大量逻辑的方法,效果很好。使用许多较小的方法,每次访问本地成员时,它的大小(所有这些访问器方法)最终胜过避免 this 的好处。仍然 - 它的确导致更清洁的功能逻辑。


My typical JS class structure looks like this:

MyClass = function(args)
{
   this.myProp1 = undefined;
   this.myProp2 = args[0];
   //...more member data 

   this.foo = function()
   {
       return this.myProp1 + this.myProp2; //<- the problem.
   }
   //...more member functions
}

//if MyClass extends a superclass, add the following...
MyClass.prototype = Object.create(MySuperClass.prototype);
MyClass.prototype.constructor = MyClass;

...My ongoing annoyance with JS is that I seem to have to use this continuously in member functions in order to access the properties of the very same object to which those functions belong. In several other languages e.g. C# & Java, this may be safely omitted when member functions are working with member data in the same class / instance. (I realise that JS is structured fundamentally differently due to it being designed as a prototypal rather than a hierarchical inheritance language.)

To put the question another way: Is there any way to make the unspecified scope NOT point to window, but rather to the current, local value of this?

P.S. I am guessing this is a language limitation, but thought I'd check again anyway.

Adding my own answer here for posterity, because at some stage, if I'm stuck between being forced to (1) prepend this and (2) using the terrifying with, it might be useful to have a (3)rd solution.

Let's assume we do not access any member data directly; instead we have getter and setter methods for each data property.

In this case, it's possible to use the private concept as discussed by Doug Crockford:

MyClass = function()
{
   var prop = undefined;
   this.getProp() {return prop;}
   this.setProp(value) {prop = value;}

   //now in standard, non-getter and -setter methods, we can do...
   this.foo = function()
   {
       return prop + prop; //<- no problem: no more 'this'
   }
}

And a (4)th solution, less safe but also less verbose:

MyClass = function()
{
   var prop = this.prop = undefined; //this.prop for read only! (unsafe)
   this.setProp(value) {prop = this.prop = value;}

   //now in standard, non-getter and -setter methods, we can do...
   this.foo = function()
   {
       return prop + prop; //<- no problem: no more 'this'
   }
}

In (4), we need only setters, reducing verbosity greatly.

With few methods containing a lot of logic, this works well. With many smaller methods, its sheer size (all those accessor methods) end up outweighing the benefit of avoiding this every time we access a local member. Still -- it does lead to cleaner in-function logic.