Chiro
Chiro

Reputation: 21

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. Any better idea?

I hope it is runnable on ie 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'));

Upvotes: 0

Views: 130

Answers (3)

Rebel Rae
Rebel Rae

Reputation: 131

In Javascript, data types are declared with var and const only. 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. 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.

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.

If they're messages to display in alerts for different user actions on the front end, you're probably right on track. Good luck

Upvotes: 1

Ashish
Ashish

Reputation: 2068

Try to use const if you don't want to modify message again.

Else you can take as global variable.

And if you have repeated method name then try use let as it

execute for paticular block only.

and implement in JSON staructure.

Upvotes: 2

Hackrrr
Hackrrr

Reputation: 338

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.

Upvotes: 2

Related Questions