javascript - Why is it possible to declare properties with a string as identifier? What's under the hood when doing it? -
i've been using properties hyphens in names(ids) while because come pretty handy when setting html tag properties same name.
in javascript do:
var obj = {'prop-with-hyphen': true}; // ✓ obj['prop-with-hyphen'] = true; // ✓
how work?
because you're doing when declare obj
creating hash, js (like many languages) has native construct. basically, you're setting string key , referring later. since key string, can composed of characters want. var obj = {'78*(^&%&*#^$)i*(fg*)(&^': "foo!"};
valid (though why you'd want key that, have no idea).
as has been pointed out in comments in original question, using hyphens in variable names make subtraction operator (-
) rather troublesome use.
Comments
Post a Comment