[Pas2js] Class constant with "external" class

Michael Van Canneyt michael at freepascal.org
Sat Jan 27 11:23:14 CET 2018



On Sat, 27 Jan 2018, Mattias Gaertner wrote:

> On Sat, 27 Jan 2018 10:46:23 +0100 (CET)
> Michael Van Canneyt <michael at freepascal.org> wrote:
>
>> [...]
>> Provided it works, of course. It's not clear to me what a class var would
>> be in Javascript.
>
> Same as with normal classes. It is accessing the class instead of the
> instance. For example:
>
> type
> TForeignObject = class external 'ForeignObject'
>  class var Counter: integer;
> end;
>
> var o: TForeignObject;
>
> o.Counter:=3;
>
> becomes:
>
> ForeignObject.Counter = 3;
>
> Now all TForeignObject instances have Counter = 3.

Yes, but how would I define this in javascript ? 
Not add it to a prototype ?

function MyClass () {
   var Counter;
   this.myCounter = Counter;
}

does not define MyClass.Counter

Excerpt from debuger console in browser:

function MyClass () {
   var Counter;
   this.myCounter = Counter;
}
undefined
MyClass.Counter = 3
3
a = new MyClass();
Object { myCounter: undefined }
console.log(a.myCounter);

but this seems to work:

function MyClass () {
   var Counter;
   this.myCounter = MyClass.Counter;
}
MyClass.Counter = 3
3
a = new MyClass();
Object { myCounter: 3 }


A bit obtuse...

Michael.


More information about the Pas2js mailing list