strawman for the := operator

Allen Wirfs-Brock allen at wirfs-brock.com
Wed Aug 8 10:08:22 PDT 2012


On Aug 8, 2012, at 9:32 AM, Rick Waldron wrote:

> 
> 
> On Mon, Aug 6, 2012 at 6:44 PM, Allen Wirfs-Brock <allen at wirfs-brock.com> wrote:
> Based upon discussions last week in the "July 25, 2012 - TC39 Meeting Notes" thread, I've created a new strawman proposal for a := operator.  See http://wiki.ecmascript.org/doku.php?id=strawman:define_properties_operator 
> 
> := is a convient way to copy properties from one object to another or to extend an object with new properties.  It combines supports for many of the same use cases as the previously proposed "object extension literals" and the JSFixed Object.extend proposal.
> 
> The most important characteristic of := is that it uses [[DefineOwnProperty]] semantics rather than [[Put]] semantics to define properties on its target object so it doesn't run into issues with assignment to accessor properties or over-riding inherited readonly properties. It is also smart about dealing with methods that reference super.
> 
> Some basic examples:
> 
>   target := src;  //define all own properties of src onto target
> 
>    //add a method and an accessor to an existing prototype
>   Point.prototype := {
>    plus(aPoint) {return new this.comstructor(this.x+aPoint.x,this.y+aPoint.y},
>    get rho() {return Math.sqrt(this.x*this.x+this.y*this.y}
> };
> 
> 
> Have at it,
> 
> Allen, 
> 
> Based on the semantics, specifically 4.I - 4.III, this doesn't work:
> 
> var div = document.createElement("div");
> 
> // Create a new div that will be hidden 
> div := {
>   innerHTML: "<p>P child element that contains a Text node</p>",
>   id: "my-div",
>   hidden: true
> };
> 
> 
> ...This is a pretty major use case that's satisfied by today's "extend" implementations.

The above is an assignment use case, not a definitional use case.  For this you want something that uses [[Put]] semantics like http://wiki.ecmascript.org/doku.php?id=strawman:batch_assignment_operator.

There really is a difference between [[Put]] and [[DefineOwnProperty]] semantics and when we tried to combine them in http://wiki.ecmascript.org/doku.php?id=strawman:object_extension_literals it  din't work very well.

A key point about := is that devs need to be intentional about whether they mean  [[Put]] or [[DefineOwnProperty]].  They need to think about the differences and what they are actually trying to do with a specific piece of code.  Part of the motivation of using two similar looking operators is to encourage devs to think "does this need to be a + or a := ?"

Currently, the convenience of = relative to the inconvenience of Object.defineProperty encourages devs to use = when they really intend [[DefineOwnProperty]] semantics. In some cases this does something other than was actually intended.

From your response, I infer we may also need to be concerned that the convenience of  := for "setting" multiple property values may encourage dev to use := when they really intend =/[[Put]] semantics.  

Allen
















> 
> I took a crack at implementing your semantics as "Object.extend" to feel out any issues or "gotchas" and came up with a few. I've posted the impl as well as a few relevant tests in this gist:
> 
> https://gist.github.com/3296375
> 
> (NOTE: The super binding is probably wrong or inadequate)
> 
> 
> I have a feeling that this may be overly complex, but I think that LeftHandSideExpression accessors should be enforced unless AssignmentExpression is defining new accessors of the same name. So, if the AssignmentExpression contains a _data_ property of the same name as an existing property in the LeftHandSideExpression that is an accessor, the value of the AssignmentExpression's data property should be assigned, not defined.
> 
> I hope that makes sense.
> 
> 
> Rick
> 
> 
> 
> 
> 
>  
> 
> Allen
> 
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120808/6722d720/attachment.html>


More information about the es-discuss mailing list