简体   繁体   中英

What is the best way to store constant values in JavasciptMVC

I know there are no constants in JavaScript however I would like to separately store configuration values to make them easy to find and tweak.

  1. Create a separate $.Class and store the key-value pairs in the static part
  2. Or have them read from a JSON configuration file
  3. Or something better

I would go with option #2. I guess JSON is more readable than extending a class with static properties.

However, I would not read the JSON in with a XHR, but just design it as JSONP. The JSON then practically gets an object literal, which can be assigned to any static variable of your application. You even could build static class properties from it by using a padding function like $.extend(MyClass, {…}); .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM