strawman for the := operator

Allen Wirfs-Brock allen at wirfs-brock.com
Thu Aug 9 09:48:35 PDT 2012


On Aug 8, 2012, at 9:36 PM, Axel Rauschmayer wrote:

>> I still find the difference between assigning and defining very subtle. I am still trying to fully wrap my head around it.
>> 
>> The best way to think about it is to think of how innerHTML works. If you "define" a property called "innerHTML", it would pave over the existing innerHTML and all of the semantics would be lost. When you "assign" to elem.innerHTML, the accessor semantics handle updating the DOM with the value given as the AssignmentExpression on RHS.
>> 
>> Hopefully that helps?
> 
> Thanks. With properties, we are missing the clarity that we have with variables: You can’t assign to an undeclared variable (in strict mode). With properties, assignment “auto-declares” if there is no own property and no setter. So things are less clear.

I think this lack of clarity may be a symptom of thinking about JS programming tasks at too low of a level.

Here is how I would make the distinction:

In JavaScript we deal with abstractions that are represented as objects. These abstractions are distinguished  by the properties they expose and their associated behaviors.

In some parts of a program you are defining new abstractions (either by creating new abstractions from scratch or by extending existing abstractions).  As you are designing such abstractions you think about possibilities.  What role  does this abstraction serve in the larger context of my program.  How will its consumers interact with it. What do I need to define to enable those possibilities.

In other parts of a program you are interacting with concrete instances of abstractions that were perviously defined either by you or by somebody else.  You are using the prepackaged behaviors provided by the abstraction (as designed by its creator). You think about, how can I use the available behaviors of this and other available abstractions to solve the problem at hand. You think about what does this abstraction represent and what can it do for me.

Object literals, class declarations, Object.define* methods, and construtor functions, and the proposed  := operator are language constructs that are commonly used in the parts of a program that define new abstractions. They are used to build abstractions.

Property access, method invocations, and property assignment (=) are language constructs that are commonly used in the parts of programs that are interacting with instances of previously defined abstractions. They are used to interact with abstractions.

If you think about JS programming in this way the distinction between = and := seems quite clear.

For DOM elements and innerHTML it comes down to deciding which of the following applies:
   1)  Do I want to update the innerHTML state of a DOM element instance?  I need to use =
   2)  Do I  want to define a new kind of DOM element with unique innerHTL behavior?  I need to use :=

There are irregularities around the edges such as when assignment auto defines a missing property but they are of minor importance if a programmer clearly understands where they are defining abstractions and where they are consuming abstractions.

Allen

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120809/fa65bbbd/attachment-0001.html>


More information about the es-discuss mailing list