Вопрос

I have an AJAX function that gets JSON response. I want to convert this JSON repsonse into a global object using prototype. I don't have a clear concept of prototyping but I think my idea is correct. Forgive me if I am wrong.

Here is the code

var RatesGlobal = {};

$('#tarrif').load("<?php echo base_url(); ?>index.php/rates/index", function (data) {

    RatesGlobal.prototype.tarrifRates = JSON.parse(data);

});    

I want to later access the values of the JSON string, forgive me if the question is not clear enough. I don't know if this is the proper usage of prototypes. What is the right way of doing so ?

Это было полезно?

Решение

You don't need any prototype. Prototypes are usually used for code reuse.

Use:

$('#tarrif').load("<?php echo base_url(); ?>index.php/rates/index", function (data) {
    window.GLOBAL = JSON.parse(data);
});

Access it this way: window.GLOBAL.

You can create object, which prototype is the response of your request:

$('#tarrif').load("<?php echo base_url(); ?>index.php/rates/index", function (data) {
    window.GLOBAL = Object.create(JSON.parse(data));
});

Anyway, I don't see why you would ever need this.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top