design patterns - Correct JavaScript 6 type to use for c# equivalent of struct -
i'm still getting used working javascript after many years of working in c#.
i have object i'd instantiate. object has same 7 fields in it. it's used pass set of 7 numbers around various functions.
var myobj = { 4: 0, 6: 0, 8: 0, 10: 0, 12: 0, 20: 0, 100: 0 }; in emcascript 6, classes have been defined, i've found on web, classes seem used defining functions on class, similar prototype of emcascript 5 function, , not defining fields.
what's proper way this?
my thinking right it's like:
var myobj = function () { this[4] = 0; this[6] = 0; this[8] = 0; this[10] = 0; this[12] = 0; this[20] = 0; this[100] = 0; }; var obj = new myobj(); i don't believe matter of opinion. in languages, there right way this.
the way in c# is
struct myobj { public int fours { get; set; } public int sixes { get; set; } public int eights { get; set; } public int tens { get; set; } public int twelves { get; set; } public int twenties { get; set; } public int hundreds { get; set; } }
to create object in javascript similar struct specified , able reuse object, try creating function returns template of object:
function createmyobj() { return { "4": 0, "6": 0, "8": 0, "10": 0, "12": 0, "20": 0, "100": 0 }; } there many different creational patterns in javascript, depends on needs. need object hierarchy? use prototype pattern. need object creation takes in default paramaters, sort of constructor? use implementation similar 1 specified above modify accept defaultvalues object contains or of properties need specifiy.
Comments
Post a Comment