Ever thought of using Enum in JavaScript? Probably every developer thinks about it. I wanted to use two dimensional JavaScript array similar to Enum in C# and also wanted to use it as key value pair. And there is a way by which you can use it like Enum and then like a key value pair.
Let's declare an array first.
var browsers = { "Microsoft": "IE", "Google": "Chrome", "Apple": "Safari", "Opera": "Opera" };
This array lists companies and their browsers. My aim is to use it like Enum and then retrieve browser's name when company name is selected.
To get a browser by Microsoft, let's use following syntax:
var msBrowser = browsers.Microsoft;
You can observe that the syntax is very similar to Enum and it returns the value 'IE'.
This article might be very foolish for many. But may help learners like me. Who are you?
Let's declare an array first.
var browsers = { "Microsoft": "IE", "Google": "Chrome", "Apple": "Safari", "Opera": "Opera" };
This array lists companies and their browsers. My aim is to use it like Enum and then retrieve browser's name when company name is selected.
To get a browser by Microsoft, let's use following syntax:
var msBrowser = browsers.Microsoft;
You can observe that the syntax is very similar to Enum and it returns the value 'IE'.
This article might be very foolish for many. But may help learners like me. Who are you?
you calling this a trick? creating an object and accessing its elements?
ReplyDeleteAnonymous,
DeleteDude please read last line of this article. For learners like me this is something new and as good as trick. If you are already aware of this then please ignore. This article is not meant for experts like you :)