Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   как сделать массив функций или функция с индексом (https://javascript.ru/forum/misc/59947-kak-sdelat-massiv-funkcijj-ili-funkciya-s-indeksom.html)

sovsem-nub 04.12.2015 11:40

как сделать массив функций или функция с индексом
 
есть очень много похожих функций

var J50Npi1={currentScript:null,getJSON:function(b,d,h){var g=b+(b.indexOf("?")+1?"&":"?");var c=document.getElementsByTagName("head")[0];var a=document.createElement("script");var f=[];var e="";this.success=h;d.callback="J50Npi1.success";for(e in d){f.push(e+"="+encodeURIComponent(d[e]))}g+=f.join("&");a.type="text/javascript";a.src=g;if(this.currentScript){c.remov eChild(currentScript)}c.appendChild(a)},success:nu ll};


var J50Npi2={currentScript:null,getJSON:function(b,d,h){var g=b+(b.indexOf("?")+1?"&":"?");var c=document.getElementsByTagName("head")[0];var a=document.createElement("script");var f=[];var e="";this.success=h;d.callback="J50Npi2.success";for(e in d){f.push(e+"="+encodeURIComponent(d[e]))}g+=f.join("&");a.type="text/javascript";a.src=g;if(this.currentScript){c.remov eChild(currentScript)}c.appendChild(a)},success:nu ll};


итд

функции почти одинаковые (оличаются в двух места индексом J50Npi) запускаются тоже одинаково
J50Npi1.getJSON(url, data, arrivalPointRender);

J50Npi2.getJSON(urlCalcPost, dataCalcPost, CalcPost);

чтобы не городить кучу почти одинаковых функций, можно к ней обращаться по индексу? вот тут код целиком http://jsfiddle.net/49gb3ja8/1/
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
</head><body>

Точка прибытия: <select id="arrivalPoint"></select>

<script>
// This line taken from J50Npi.min.js (within this repo)
var J50Npi={currentScript:null,getJSON:function(b,d,h){var g=b+(b.indexOf("?")+1?"&":"?");var c=document.getElementsByTagName("head")[0];var a=document.createElement("script");var f=[];var e="";this.success=h;d.callback="J50Npi.success";for(e in d){f.push(e+"="+encodeURIComponent(d[e]))}g+=f.join("&");a.type="text/javascript";a.src=g;if(this.currentScript){c.removeChild(currentScript)}c.appendChild(a)},success:null};
var J50Npi2={currentScript:null,getJSON:function(b,d,h){var g=b+(b.indexOf("?")+1?"&":"?");var c=document.getElementsByTagName("head")[0];var a=document.createElement("script");var f=[];var e="";this.success=h;d.callback="J50Npi2.success";for(e in d){f.push(e+"="+encodeURIComponent(d[e]))}g+=f.join("&");a.type="text/javascript";a.src=g;if(this.currentScript){c.removeChild(currentScript)}c.appendChild(a)},success:null};




          



var url = 'http://emspost.ru/api/rest/?method=ems.get.locations&callback=arrivalPointRender';
var data = {plain: "true", type:"russia"};

// We need a function callback to be executed after the response is received
var arrivalPointRender = function(data){
    var html = '', selected = false;

       for(var i = 0; i < data.rsp.locations.length; i++){
      
         if(data.rsp.locations[i].type=="cities") {data.rsp.locations[i].type="(город)"};
            if(data.rsp.locations[i].type=="regions") {data.rsp.locations[i].type="(регион)"};
       
           html += '<option value="'+data.rsp.locations[i].value+'"'+(selected?'':' selected')+'>'+data.rsp.locations[i].name+' '+data.rsp.locations[i].type +'</option>'
        selected = false    
                                                         
     }
 document.getElementById('arrivalPoint').innerHTML = html;   
    }
J50Npi.getJSON(url, data, arrivalPointRender);




var urlCalcPost = 'http://emspost.ru/api/rest?method=ems.calculate&callback=CalcPost&from=city--moskva&to=region--omskaja-oblast&weight=1.5';
var dataCalcPost = {plain: "true"};
var CalcPost = function(dataCalcPost){ 
  
 console.log(dataCalcPost.rsp);
  alert(" нужно чтобы был 1 алерт. Цена "+ dataCalcPost.rsp.price);
   
};

J50Npi2.getJSON (urlCalcPost, dataCalcPost, CalcPost);
</script>

</body>
</html>

sovsem-nub 04.12.2015 14:15

вот так вроде нормально http://jsfiddle.net/49gb3ja8/3/


Часовой пояс GMT +3, время: 22:54.