2017-03-02 2 views
0

Kann mir jemand diesen Javascript/Ajax-Code erklären? Ich verwende diesen Code zu FileManager (mit jsTree).JavaScript-Konstrukt erklären

this.files; 
this.file_container; 

var obj = this; 

$.ajax 
({ 
    url: 'ajax_requests.php?link=allFile', 
    success: function (result) 
    { 
     obj.construct(JSON.parse(result)); 
    } 
}); 

this.construct = function construct (files) 
{ 
    this.files = files; 
    this.file_container = $('#file_container'); 

    this.listFiles(this.files, this.file_container); 
}; 
+0

ist dieser Code allein in einer Datei oder ist das ein Fragment? –

+0

Es scheint Teil eines Objekts/Moduls zu sein, das einen AJAX-Aufruf für eine JSON-Datei ausführt und dann die Eigenschaften files und files_container selbst auf das Ergebnis setzt, indem es eine eigene Konstruktmethode verwendet, die unterhalb des AJAX-Aufrufs hinzugefügt wird. Wenn es fertig ist, führt es seine eigene Funktion listFiles aus, die diese Eigenschaften als Parameter verwendet. Die oberen beiden Zeilen 'this.files' und' this.file_container' machen eigentlich nichts. – Shilly

Antwort

0

Nun, ich nehme an, dass dieser Code ein Fragment eines Moduls ist. Wenn es alleine in einer Datei ist, ist das "das". wird nicht viel Sinn machen.

this.files;    // these 2 lines declarations of properties of 
this.file_container;  // the module. They aren't necessary once the 
         // properties are created on first assign, 
         // but it could be justa way of make code 
         // more clear/readable 

var obj = this; 

$.ajax // this is an ajax call to... 
({ 
    url: 'ajax_requests.php?link=allFile', // ... this url ... 
    success: function (result) //...which calls this function on success... 
    { 
     obj.construct(JSON.parse(result)); 
     //...then, as obj = this, calls contruct method with the JSON parsed 
     // ajax's call result. So the ajax call returns a JSON that is 
     // transformed to object by the JSON.parse method. Then this object is 
     // used as parameter to construct method. 

    } 
}); 

this.construct = function construct (files) 
{ 
    this.files = files; 
    // assigns previous declared files property to the result of ajax call 
    this.file_container = $('#file_container'); 
    // assigns previous declared file_container property to the jscript object 
    // representing the DOM element whose id is 'file_container'. 

    this.listFiles(this.files, this.file_container); 
    // calls a method called listFiles (which is not present in this fragment), 
    //having as parameters the 2 properties files and file_container. 
}; 

Falls Sie nicht wissen, was AJAX ist, überprüfen Sie diese: What is AJAX, really?