解释JavaScript构造
有人能给我解释一下这个javascript/ajax代码吗?我使用这段代码创建FileManager(带有jsTree)解释JavaScript构造,javascript,ajax,Javascript,Ajax,有人能给我解释一下这个javascript/ajax代码吗?我使用这段代码创建FileManager(带有jsTree) 我假设这段代码是一个模块的片段。如果文件中只有这一个,那么“这个”就没什么意义了 this.files; // these 2 lines declarations of properties of this.file_container; // the module. They aren't necessary once the
我假设这段代码是一个模块的片段。如果文件中只有这一个,那么“这个”就没什么意义了
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是什么,请检查:这段代码是单独存在于文件中还是一个片段?它似乎是对json文件进行AJAX调用的对象/模块的一部分,然后使用添加在AJAX调用下面的ITA自己的构造方法将其自身的文件和文件容器属性设置为结果。完成后,它将使用这些属性作为参数执行自己的listFiles函数。顶部的两行,
this.files
和this.file\u容器
实际上什么都不做。
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.
};