我尝试了JavaScript中的类也使用extend.js来创建它们。我在课上遇到了一些麻烦,我不太确定我是否需要它们,但我认为这将是有用的。这是一个只获取一些数据库表数据的类。如何通过函数为类提供选项列表?
var TableData = Class.extend(function(){
this.rowId;
this.action;
this.returnWhat;
this.tableType;
this.php;
this.query = function(){
if (this.returnWhat) this.action += '_' + this.returnWhat;
var postData = { func : this.action };
if (this.rowId) $.extend(postData, { rowId : this.rowId });
$.post(this.php, postData, function(data){
if(data){this.data = data;}
});
return this.data;
};
});
我只说我知道的表,所以我只是并将其作为增值经销商/选项,但我不知道如何设置类正确瓦尔起来。这例如不起作用,但可能证明我正在尝试做什么。
var options = {
rowId : '*',
action : 'get',
returnWhat : 'all'
}
var league = function (options){
this.tableType = 'league';
this.rowId = options.rowId;
this.action = options.action;
this.returnWhat = options.returnWhat;
this.php = 'php/' + options.tableType + '.php';
}
var tableData = new TableData(assoc_league);
嗯,这是一个疯狂的方式来实现简单的东西。加var联盟,你可能会更好地设置this.php使用this.tableType,否则在你的例子中,它将是undefined – Christian 2014-10-06 00:05:48
是的,我认为是这样。我不知道的是,如果它是因为我使用类错误的概念,或者因为我不应该使用类。 – Tester232323 2014-10-06 00:07:25
也是$ .post是异步,所以返回this.data,将不会返回任何东西 – Christian 2014-10-06 00:08:16