2017-03-02 116 views
0

任何人都可以解釋我這個javascript/ajax代碼嗎?我使用此代碼FileManager(與jsTree)。解釋JavaScript構造

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

是這個代碼單獨在一個文件或者這是一個片段? –

+0

它似乎是爲ajax調用json文件的對象/模塊的一部分,然後使用自己的構造方法將其自身的文件和files_container屬性設置爲結果,並將其添加到ajax調用的下方。完成後,它使用這些屬性作爲參數執行自己的listFiles函數。最上面的兩行「this.files」和「this.file_container」實際上並沒有做任何事情。 – Shilly

回答

0

那麼,我假設這段代碼是一個模塊的片段。如果它是一個文件中的「這個」。沒有多大意義。

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. 
}; 

在你不知道AJAX是什麼情況,檢查:What is AJAX, really?