无论如何通过变量访问es6常量?

时间:2016-12-23 02:41:05

标签: javascript syntax ecmascript-6

无论如何通过变量访问ES6常量?

对于前。

const OPEN_TAB = 0;
const CLOSE_TAB = 1;

let action = 'OPEN';

console.log(window[`${action}_TAB`]); <-- something like that

2 个答案:

答案 0 :(得分:9)

没有(*)。 const声明不会成为全局对象的属性。

您需要找到另一种解决方案,例如创建一个对象并将其冻结(使其不可变):

const TAB = Object.freeze({
  OPEN: 0,
  CLOSE: 1,
});

console.log(TAB[action]);

我认为依赖全局变量(即var)成为全局对象的属性无论如何都是糟糕的设计。如果你想通过名字查找一些东西,你真的应该有地图或记录(如上所示)。

*:嗯,您可以使用eval ...

答案 1 :(得分:0)

对于您拥有的当前代码,您可以使用const OPEN_TAB = 0; const CLOSE_TAB = 1; let action = 'OPEN'; console.log( eval (action+'_TAB') ); But take care!,它应该是这样的:

const

另一种方法是假设const的新对象,然后您可以像访问JavaScript对象的常用方式一样轻松访问const TAB = { OPEN:0, CLOSE:1 }; let action = 'OPEN'; console.log(TAB[action]);

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NetTelegramBotApi;
using NetTelegramBotApi.Requests;
using NetTelegramBotApi.Types;
using System.Net.Http;
using System.Runtime.Remoting.Channels;
using System.Data;
using System.Data.SqlClient;

namespace TestMyCam_bot
{


    class Program
    {
        private static string Token = "";
        private static ReplyKeyboardMarkup mainmenu;


        int i, j;
        int Counter = 0;



        bool success = false;
        bool bool_EndHtml = false;
        bool yek_Bar_Downloaded = false;


        Exception Ex;

        static void Main(string[] args)
        {

            mainmenu = new ReplyKeyboardMarkup 
            {

                Keyboard = new[] { new[] { "Sony" }, new[] { "Apple" }, new[] { "Nokia"} } 


            };

            Task.Run(() => RunBot());
            Console.ReadLine();
        }


        public static async Task RunBot()
        {





            var bot = new TelegramBot(Token);
            var me = await bot.MakeRequestAsync(new GetMe());
            Console.WriteLine("User Name is {0}", me.Username);
            long offset = 0;
            int whilecount = 0;
            while (true)
            {

                Console.WriteLine("while is {0}", whilecount);
                whilecount += 1;
                var updates = await bot.MakeRequestAsync(new GetUpdates() { Offset = offset });
                Console.WriteLine("Update Count is {0} ", updates.Count());
                Console.WriteLine("-------------------------------------");
                try
                {
                    foreach (var update in updates)
                    {

                        offset = update.UpdateId + 1;
                        var text = update.Message.Text;
                        if (text == "/start")
                        {
                            var req = new SendMessage(update.Message.Chat.Id, "Select Button") { ReplyMarkup = mainmenu};

                            await bot.MakeRequestAsync(req);
                            continue;

                        }



                }
                catch (Exception ex)
                {
                    throw;
                }

            }
        }
    }
}