Subscribe to my email list now at http://jauyeung.net/subscribe/
Follow me on Twitter at https://twitter.com/AuMayeung
Many more articles at http...
For further actions, you may consider blocking this person and/or reporting abuse
Another hidden ability of JavaScript JSON is
Date.prototype.toJSON
, which will customizeJSON.stringify
behavior of the object.Thanks for finding that.
That's a method that most people don't think of using.
Just so you know, including multiple return paths for functions is an anti-pattern/code-smell. It is a best practice for a function to only have a single return path if that is possible. So, for example, in your third to last snippet, you could have written the
replacer
function like this instead and have what it returns be the same (I also fixed your NaN check to use Number.isNaN() because using equality to check against NaN will not work as expected):Absolutely wrong! In every case is switch more readable than joined or/and superlongline. More easy to update. And you will not make error in it so easily like in bunch of writeonly code you just wrote.
I think ternary operator shouldn't be nested. It's harder to read than if or switch like you said.
value === NaN
is always false. For exampleNaN === NaN
. Use isNaN().Thanks. That's a good catch.
You should actually be using Number.isNan() over the global isNaN function which will give somewhat unexpected results in comparison to the newer method on the Number object. You should also use !Number.isFinite() instead of checking equality with Infinity.
Thanks. That's also a good tip.
I think equality is still good as long as we use triple equals.
Yes, you can still write it that way.
Also if the object or class that you're converting has a toJSON function, it will use that to determine the output - can be very handy :)
developer.mozilla.org/en-US/docs/W...
Yea. Then we don't have to passing a mapping function every time we want to stringify. I think URL and URLSearchParams objects have this method.