2017-03-02 65 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?