Implementing Polymorphism in Javascript - how would this look? -
i'm trying make jump more oop style javascript approach, have not gotten right in javascript.
take following function example.
function positionalcss(array, cs, lcs){ /* define css circle based on number of circles */ //count array var arrcount = array.length; var t = []; var l = []; if(arrcount == 3){ t[0] ='15px'; l[0] = '240px'; t[1] = '345px'; l[1] = '440px'; t[2] = '345px'; l[2] = '40px'; } if(arrcount == 4){ t[0] ='-135px'; l[0] = '90px'; t[1] = '-10px'; l[1] = '290px'; t[2] = '220px'; l[2] = '270px'; t[3] = '315px'; l[3] = '90px'; } if(arrcount == 6){ t[0] ='-135px'; l[0] = '90px'; t[1] = '-10px'; l[1] = '290px'; t[2] = '220px'; l[2] = '270px'; t[3] = '315px'; l[3] = '90px'; t[4] = '210px'; l[4] = '-100px'; t[5] = '-10px'; l[5] = '-110px'; } $.each(array, function(i) { var num = parseint(i); // console.log('$("' + lcs + ' ' + cs + '.handle-' + num + '").first().children("div");'); $(lcs + ' ' + cs + '.handle-' + num).first().children('div').css({ 'position': 'absolute', 'top': t[num], 'left': l[num] }); });
}
it's pretty horrendous, want pass in array, , depending on how many there are, organise positions of items based on this. guess based on size give properties? each tl represents top , left position of object?
i'd create object can lookup premade arrays of objects holding t/l
values.
var counts = { 3: [ {t:'15px', l:'240px'}, {t:'345px', l:'440px'}, {t:'345px', l:'40px'} ], 4: { // above }, 5: { // above } };
and use in function:
function positionalcss(array, cs, lcs){ $.each(counts[array.length], function(i, obj) { // console.log('$("' + lcs + ' ' + cs + '.handle-' + + '").first().children("div");'); $(lcs + ' ' + cs + '.handle-' + i).first().children('div').css({ 'position': 'absolute', 'top': obj.t, 'left': obj.l }); }); }
Comments
Post a Comment