dimanche 5 mars 2017

Are classes methods created individually for instances?

I want to know whether in ActionScript 3 there's a way to share a same function (method) between the instances of a class definition, only referencing the same function everytime... i.e., this example should log true, but logged false (note: I'd want this to reduce duplicating functions).

class A {
    function f() {}
}

trace(
    (new A).f === (new A).f
)

An ActionScript 3 language specification appears to say that a prototype attribute exists, but not implemented. I've understood that individual classes have a prototype object. I've specially found a prototype property (probably inherited from Class/Object) and wonder if classes use meta functions in order to be constructed (since their type is "object"... when I test with typeof: trace(typeof A, typeof Class, typeof Object)).

My last try:

class A {}

A.prototype.f = function() {}

trace(
      (new A).f === (new A).f
)

It says that f doesn't exist. I could define this class as a function instead (in order to move methods to the prototype object):

function A() {}

A.prototype.f = function() {}

, but in this way I can't control access of instance members.

Aucun commentaire:

Enregistrer un commentaire