简体   繁体   English

我需要通过Javascript建立消息存储。 我应该使用什么结构?

[英]I need to build a Message Storage by Javascript. What structure should i use?

I build a message storage (message constant in javascript) my idea is build a class like below but using var seem not good. 我建立了一个消息存储(JavaScript中的消息常量),我的想法是建立一个像下面这样的类,但是使用var似乎不好。 Any better idea? 有更好的主意吗?

I hope it is runnable on ie 11 我希望它可以在11上运行

var MessageLibrary= (function() {
     var Messages= {
         'Hello': 'Hello from other side',
         'Bye': 'Say goodbye bye'
     };
     return {
        get: function(name) { return Messages[name]; }
    };
})();
alert(MessageLibrary= .get('Hello'));

Try to use const if you don't want to modify message again. 如果您不想再次修改消息,请尝试使用const。

Else you can take as global variable. 否则,您可以将其作为全局变量。

And if you have repeated method name then try use let as it 如果您重复了方法名称,请尝试使用let作为它

execute for paticular block only. 仅对特定的块执行。

and implement in JSON staructure. 并以JSON staructure实现。

For most things that I did, I used two-dimenzional arrays (arrays in array). 对于我所做的大多数事情,我都使用二维数组(数组中的数组)。 You can use it too - something like this: 您也可以使用它-像这样:
var messages = [["User1", "12:00 12. 12.", "Hello"],["User2", "12:02 12. 12.", "Greetings"],["User1", "9:48 13. 12.", "How are you"]]
Or if you want only messages, no another info about message, you can use simple array. 或者,如果只需要消息,而没有消息的其他信息,则可以使用简单数组。

In Javascript, data types are declared with var and const only. 在Javascript中,仅使用varconst声明数据类型。 Objects are declared the same as strings, integers, boolean values. 声明的对象与字符串,整数,布尔值相同。 if you need to find the type of an extant variable or expression, prepend typeof . 如果需要查找现存变量或表达式的类型,请在typeof All variables of all types are undefined until they're defined and then they're of a return type until you nullify it with null . 在定义它们之前,所有类型的所有变量都是undefined直到返回null为止,它们都是返回类型。

See below 见下文

var integer; // undefined type, undefined value
typeof integer; // returns undefined
const word; // undefined type, undefined value
integer = ""; // string type, "" value
typeof integer; //returns string
word = 12; // int type, undefined value
var integer = null; // null type, undefined value
etc...

If your concern is with your data structure, that's entirely up to you to suit your own needs. 如果您关心数据结构,则完全取决于您自己的需要。

It seems you're trying to structure data between users? 似乎您正在尝试在用户之间构建数据? If that's the case, you should spend some good time learning server structure, databasing, encryption, op-sec, and a few other things before diving into messaging. 如果是这样,您应该花一些时间学习服务器结构,数据库基础,加密,op-sec以及其他一些内容,然后再深入研究消息传递。

If they're messages to display in alerts for different user actions on the front end, you're probably right on track. 如果它们是要在前端针对不同用户操作显示在警报中的消息,那么您可能正好步入正轨。 Good luck 祝好运

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript。 我正在尝试使用.findIndex(),但收到以下消息: TypeError: a is not a function - Javascript. I am trying to use .findIndex() but I get the following message: TypeError: a is not a function 我正在将php变量传递给javascript。 有没有更好的方法来构造它? - I'm passing php variables to javascript. Is there a better way to structure this? PHP和JavaScript中的变量。 我需要基本了解方面的帮助。 - Variables in php and JavaScript. I need help on basic understanding. 我在 JavaScript 中的 Pluralsight using.has() 上看到了这个问题。 那是什么? - I saw this question on Pluralsight using .has() in JavaScript. What is that? Ajax无法使用javascript。 我应该做些什么? - Ajax not working with javascript. What am I supposed to do? 我应该在这个练习中使用 JavaScript 中的哪个函数? - What function in JavaScript should I use for this exercise? 我应该使用什么工作流程进行JavaScript编辑? - What workflow should I use for JavaScript editing? 解析Javascript。 尝试使用我从查询中提取的数据 - Parse Javascript. Trying to use the data I pulled from a query 当我将鼠标放在 JavaScript 上时,我需要重新定位方块。 我怎么做? - I need to reposition the squares when I put the mouse over JavaScript. How do I do that? 我需要打印div并在Javascript中添加数组内容。 我怎么做? - I need to print divs and appending array contents inside of them in Javascript. How do i do that?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM