+1

I desperately want this in the language.

AJ ONeal

On Tue, Jun 12, 2012 at 11:14 AM, Rick Waldron <[email protected]>wrote:

>
>
> On Tue, Jun 12, 2012 at 11:45 AM, Ryan Florence <[email protected]>wrote:
>
>> I use ||= very regularly in ruby and coffeescript, both of which have
>> default arguments.
>>
>> I definitely agree that default arguments are a decent alternative. I
>>> can't recall examples where it wouldn't be enough. Do you have use cases
>>> where you would use ||= and default argument values couldn't be used?
>>>
>>
>> Its super handy for caching and late/dynamic initialization of object
>> properties.
>>
>>
>> var events = {
>>
>>   _callbacks: {},
>>
>>    on: function (topic, callback) {
>>     (this._callbacks[topic] ||= []).push(callback);
>>     ...
>>   },
>>   ...
>> };
>>
>>
>> It's also useful for settings object defaults, especially for expensive
>> settings that you don't want to calculate in some `defaults` object that is
>> merged into the settings.
>>
>> function ajaxWithError (settings) {
>>   settings.method ||= 'POST';
>>
>>   settings.elementPosition ||= this._getElementPositions();
>>
>>   settings.somethingWithInitialization ||= (function(){
>>     var thing = new Thing();
>>     thing.foo = settings.foo;
>>     return thing;
>>   })();
>>   ...
>> };
>>
>>
>> The ||= (function(){})() should remind any rubyist of `||= begin ... end`
>> which is beloved by many.
>>
>> So yeah, I think its really useful outside of default arguments.
>>
>> - Ryan Florence
>>
>>
> Forgive the dogpiling, but these are really excellent real-world use cases
> that represent massive pain points in modern JavaScript programming. Thanks
> for writing these up Ryan, I've added them to strawman:
> http://wiki.ecmascript.org/doku.php?id=strawman:default_operator
>
> Rick
>
> _______________________________________________
> es-discuss mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/es-discuss
>
>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to