Consider the following code:
var table = function () {
return {
_init: function (tableId) {
this.tableId = tableId;
this.table = $("#" + tableId);
},
removeStripes: function() {
this.table.find(".alt"开发者_如何学编程).removeClass("alt");
},
addStripes: function () {
this.table.find("tbody tr:even").addClass("alt");
},
resetStripes: function() {
this.removeStripes();
this.addStripes();
},
sort: function (options) {
this.table.tablesorter(options);
this.table.bind("sortEnd", this.resetStripes);
},
}
}
var newTable = new table();
newTable._init("tableId");
var options = {};
newTable.sort(options);
The browser says this.removeStripes called by this.resetStripes, on one of the last lines) is not a function. I guess there is some error with this
, but I can't figure out where it is. Any idea, anyone?
Try removing coma (,) after declaration of sort function. This will probably not solve your problem, but it will 'compile' under ie<7.
As you are declaring methods with anonymous functions, this
is not anymore in the table
function scope.
You should use:
removeStripes: function() {
// ...
}.bind(this)
on all of your methods.
What your looking for is a constructor method/pattern. Now when you call one of the instantiated objects methods (eg. newTable.removeStripes() ) the context (this) is set to newTable. I believe you can always change the context however with newTable.removeStripes.call(someOtherTableOrObject) which comes in handy in a lot of cases.
function Table(tableId) {
this.tableId = tableId;
this.table = $("#" + tableId);
this.removeStripes = function() {
this.table.find(".alt").removeClass("alt");
};
this.addStripes = function () {
this.table.find("tbody tr:even").addClass("alt");
};
this.resetStripes = function() {
this.removeStripes();
this.addStripes();
};
this.sort = function (options) {
this.table.tablesorter(options);
this.table.bind("sortEnd", this.resetStripes);
};
}
var newTable = new Table("tableId");
var options = {};
newTable.sort(options);
var table = function (tableId) {
this.tableId = tableId;
this.table = $("#" + tableId);
this.removeStripes= function() {
this.table.find(".alt").removeClass("alt");
};
this.addStripes= function () {
this.table.find("tbody tr:even").addClass("alt");
};
this.resetStripes= function() {
this.removeStripes();
this.addStripes();
};
this.sort= function (options) {
this.table.tablesorter(options);
this.table.bind("sortEnd", this.resetStripes);
};
}
The "this" keyword usage in Javascript is rather different than in other languages. The version above should work (I tested it with jsc, rather than properly in a browser with jQuery). Now the function is a proper constructor, making the interpreter to set "this" to the object to be created.
Try this:
var table = function () {
var that = this;
return {
_init: function (tableId) {
that.tableId = tableId;
that.table = $("#" + tableId);
},
removeStripes: function() {
that.table.find(".alt").removeClass("alt");
},
addStripes: function () {
that.table.find("tbody tr:even").addClass("alt");
},
resetStripes: function() {
that.removeStripes();
that.addStripes();
},
sort: function (options) {
that.table.tablesorter(options);
that.table.bind("sortEnd", that.resetStripes);
},
}
}
var newTable = new table();
newTable._init("tableId");
var options = {};
newTable.sort(options);
Its a common pattern in JavaScript to save the this
into another variable so you can reference it in deeper nested functions. As javascript this
always refers to either the global object or the this object that was passed to the function through .call or .apply it's rather unreliable to use at times..
精彩评论