Javascript ReferenceError: Invalid left-hand side in assignment
So lately was trying some silly syntax with javascript, logically it seems to work, but throw away your dry logic!
Most of us who have been pawing at some raw JS knows undefined || "hi"
would result in "hi", on the same lines, /me tried :
> obj={} Object > obj["prop"] || obj["prop"] = "newprop" ReferenceError: Invalid left-hand side in assignment arguments: Array[0] get message: function () { [native code] } get stack: function () { [native code] } set message: function () { [native code] } set stack: function () { [native code] } type: "invalid_lhs_in_assignment" <strong>proto</strong>: Error > obj["prop"] = obj["prop"] || "newprop" > obj.prop "newprop" # Works this way ^_ ^
One of my friend @Altreus said "Lack of ||= saddens me" and @jasonmulligan solution helped!
Finally:
- ||= is much needed.
- obj['prop'] ||= "fun"
- There is already a proposal in ES5 for this now!
Happy hacking, hope we shall have more fun with more JS :)
EDIT 0 suggested by charliesome:
obj["prop"] || (obj["prop"] = "newprop");
Recent blog posts
- watir-webdriver web inspector
- gem list to gemfile
- Packing ruby2.0 on debian.
- Made it into The Guinness Book!
- to_h in ruby 2.0
- Filter elements by pattern jQuery.
- Better HTML password fields for mobile ?
- Grayscale image when user offline
- nth-child CSS pseudo-class Christmas colors
- EventEmitter in nodejs