4 ms·
JavaScript has good parts, I write it a lot. But it is ignorant to close eyes on its warts 1 + '2' 1 - '2' Number.MAX_SAFE_INTEGER + 2 and entire
by sergeykish 6y ago
JavaScript has good parts, I write it a lot. But it is ignorant to close eyes on its warts
1 + '2'
1 - '2'
Number.MAX_SAFE_INTEGER + 2
and entire WAT series, stems from "don't raise" ethos. JavaScript exposes constructor instead of prototype that messed up a lot, in Ruby terms
Object.alias_method :__proto__, :class
Object = Object.instance_method(:initialize)
Class = Class.instance_method(:initialize)
Class.__proto__.alias_method :prototype, :owner
new = ->(constructor) { constructor.owner.new }
Person = Class.prototype.new do
def initialize
end
end.instance_method(:initialize)
def Person.foo
'foo'
end
puts Person.foo
john = new.call Person
def john.bar
'bar'
end
puts john.bar
def (Person.prototype).baz
'baz'
end
puts john.__proto__.baz
Does anyone wants to adopt this feature in their language?
- hajile 6y agoOperator overloading can lead to ambiguities in dynamic languages. Ruby, python, and any number of other languages have it much worse because they can be overloaded any way you want while JS overloads are (currently at least) set in stone by the language. If you could only choose one number type, would it be floats or ints? Crockford would say decimal, but the rest of use using commodity hardware would choose floats every time. It's not the language implement's fault someone doesn't understand IEEE 854. This max safe integer issue exists in ALL languages that use IEEE 854. In any case, BigInt is already in browsers and will be added to the spec shortly.
- sergeykish 6y agoPerl and Lua have separate arithmetic and concatenation operators 1 + "2" #3 1 - "2" #-1 1 . "2" | 1 .. "2" #"12" 1 . "-2" | 1 .. "-2" #"1-2" Python and Ruby throw exception, use explicit type conversion and string interpolation 1 + '2' Traceback (most recent call last): TypeError (String can't be coerced into Integer) 1 + '2'.to_i #3 "1#{'2'}" #"12" Hardly any language is perfect, I have not encounter much of operator overload in ruby (nokogiri?) but I believe C++ got it bad. One number type in Lua: > 0 | 0xffffffff 4294967295 > 0 | 0xffffffffffffffff -1 > 0 | 0x7fffffffffffffff 9223372036854775807 limited but better than JavaScript: 0 | 0xffffffff //-1 BigInt is an improvement 0n | 0xffffffffn //4294967295n 0xffffffffffffffffn 18446744073709551615n it is strict 1n - "2" 1 + 1n Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions works nice with string interpolation: `${1n}` "1" Numbers is a sane example. One can argue it was for good. How about `{} + []`? I believe I can disable this part in JavaScript engine and no one would notice. And misleading `object[key]` where it calls toString, sure I have not tried that in a decade but it is stupid. UTF-16: ""[1] # there were emoji //"�" You've said nothing about constructor oriented programming. Unique feature, I have not heard any other language adopted it yet. The post you've replied contents sketch for Ruby. Actually I've got it wrong — every JavaScript function is a closure (Ruby method is not closure) and Prototype method was a class method (not instance method), fixed but ugly: def function(&block) Class.prototype.new.tap do |c| c.define_method(:initialize, block) end.instance_method(:initialize) end def function_(object, name, &block) object.class_eval do define_method(name, &block) end end Person = function { |name| @name = name } function_(Person.prototype, :name_) { @name } john = new.call Person, 'john' puts john.__proto__ == Person.prototype puts john.name_ def function__(object, name, &block) object.singleton_class.class_eval do define_method(name, &block) end end function__(john, :name__) { @name } puts john.name__ By the way, you can say "Yes, I know JavaScript has some problems". It is not a secret, everyone knows.
- hajile 6y agoI completely agree that separate operators area MUST for dynamic languages. Lua allows operator overloading with metatables as do ruby and Python with classes http://lua-users.org/wiki/MetatableEvents http://lua-users.org/wiki/MetatableEvents https://docs.python.org/3/reference/datamodel.html#emulating-numeric-types https://docs.python.org/3/reference/datamodel.html#emulating... https://www.ruby-lang.org/en/documentation/faq/7/ https://www.ruby-lang.org/en/documentation/faq/7/ > One number type in Lua: Not quite true. Lua had only 64-bit floats like JS until version 5.3 and the blazing fast LuaJIT still only has floats. Well, to be honest, it has hidden 32-bit integers for sake of bitwise operations just like JS (well, JS uses 31-bits with a tag bit which is probably a lot faster). > How about `{} + []`? I believe I can disable this part in JavaScript engine and no one would notice. That's very simple. {} at the beginning of a line is an empty block rather than an object (yay C). "Disabling" that would break the entire language. > UTF-16 UCS-2 actually. Back in those days, Unicode was barely a standard and that in name only. Java did/does use UCS-2 and JS for marketing reasons was demanded to look like Java. I don't want to go into this topic, but python, PHP, ruby, C/C++, Java, C#, and so on all have a long history not at all compatible with UTF-8. > You've said nothing about constructor oriented programming. Unique feature, I have not heard any other language adopted it yet. I'll give you that JS prototypal inheritance is rather complex due to them trying to pretend it's Java classes. Once again though, the deep parts of both Python and Ruby classes are probably more difficult to explain. Lua's metatables are very easy to understand on the surface, but because there's no standard inheritance baked in, every project has their own slightly different implementation with it's own footguns. Closures are almost always preferred over classes in modern JS. Likewise, composition is preferred over inheritance and the use of prototype chains while not necessarily code smell, does bear careful consideration. If someone insists on using deep inheritance techniques, they certainly shouldn't be using class syntax as it adds yet another set of abstractions on top. Object.create() and inheriting from `null` solves a ton of issues. > By the way, you can say "Yes, I know JavaScript has some problems". It is not a secret, everyone knows. I'd say if you take the top 20 languages on the tiobe index, it sits in the middle of the pack with regard to warts and weirdness. Maybe people are just attracted to weird languages.
- sergeykish 6y ago