re-using getters and setters in object literals

Richard Cornford Richard at litotes.demon.co.uk
Tue Aug 19 16:55:51 PDT 2008


In the current ES 3.1 draft we have:-

PropertyAssignment :
    PropertyName : AssignmentExpression
    get PropertyName ( ) { FunctionBody }
    set PropertyName ( PropertySetParameterList ) { FunctionBody }

- in which I don't see any provision for re-using function objects
as getters and setters. That is, there is no mechanism for
defining/creating a function object elsewhere and then using it as
a getter or a setter for an object literal. Has that possibility
been rejected, or never raised?

As it stands the algorithms require a new function object be created
for each getter and setter with each object creation process that
following from the use of an object litters. This strikes me as
possibly being less than optimal in some circumstances, and
certainly would preclude the possibility of using functions
returned from the - bind - method or inner functions that
exploit closures as getters and setters in object literals (except
the closures formed by the evaluation of the PropertyAssignments).

I imagine that the productions for these possibilities would be
something like:-

    get PropertyName : AssignmentExpression
    set PropertyName : AssignmentExpression

- where a TypeError would be thrown in the event that
AssignmentExpression did not evaluate as a function object
following GetValue (i.e. is not an object or does not have a [[Call]]
method). With AssignmentExpression used here to allow, for example,
the return value from a function call to provide the function
refence value used as the getter/setter (or even - new Function( .. )
-).

Richard Cornford.



More information about the Es-discuss mailing list