|
|
|
|
|
by pmontra
4018 days ago
|
|
Symbols are used all the time in Ruby (and a bunch of other languages), not as duct tape but as a very core feature of the language. Why should it be different in JavaScript? The most simple way to use them is to replace definitions like this one var north = 1;
var south = 2;
var east = 3;
var west = 4;
var direction1 = north;
var direction2 = south;
direction1 === direction2 ? "ops" : "ok";
The programmer is doing the work of the interprer/compiler here and make sure to pick unique values for all the constants that are going to be compared together. With symbols that becomes var north = new Symbol();
var south = new Symbol();
var east = new Symbol();
var west = new Symbol();
var direction1 = north;
var direction2 = south;
direction1 === direction2 ? "ops" : "ok";
which is an improvement even if it is (in a traditional JavaScript way) so much more verbose than Ruby's direction1 = :north
direction2 = :south
direction1 === direction2 ? "ops" : "ok"
I just wish they'll add some syntactical sugar to do without that "new Symbol()" thing and create symbols as needed like Ruby does.Unfortunately, from https://developer.mozilla.org/en/docs/Web/JavaScript/Referen... Symbols and JSON.stringify()
Symbol-keyed properties will be completely ignored when using JSON.stringify():
JSON.stringify({[Symbol("foo")]: "foo"});
// '{}'
We're going to manually serialize them when they leave the RAM. |
|