在对象HrRecommendUser中this代表是HrRecommendUser的实例
var HrRecommendUser = HrRecommendUser || function () { }
HrRecommendUser.prototype.SelectAll = function (target) { alert(this); var obj = this; alert(obj); var check = $(target).attr('checked') || false; $('#tbConnectList>tbody>tr>td').find('input:checkbox:visible').attr('checked', check);}HrRecommendUser.prototype.recommedUser = function (target) {
alert(this);//此时的this是HrRecommendUser调试可以看到this包含两个方法recommedUser()和SelectAll() var obj = this; alert(obj); var boxArray = $('#tbConnectList>tbody>tr>td').find(':checkbox:checked:visible'); var userId = '['; $.each(boxArray, function (i, item) { userId += '{"UserId":"' + $(item).parents('tr:first').attr('userid') + '","RealName":"' + $(item).parents('tr:first').attr('realname') + '"},'; }); userId = $.trim(userId).substring(0, $.trim(userId).length - 1) + ']'; $('#hdfUserId').val(userId); if ($.trim($('#hdfUserId').val()).length <= 2) { popAlertMessage('请至少选择一个朋友', document.body); return false; }}var hrRecommendUser = new HrRecommendUser();
如果在 $('#tbKeyWord')
.bind('keyup change', function () { var keyWord = $(this).val(); $('#tbConnectList').filterTableItems(keyWord, { rowsGroup: 2, startRow: 1, usePinyin: false }); $('#tbConnectList>tbody').alterRowColor(); });中this代表是当前dom标签,就像我们定的C#类一样,在class内部this代表Class(可以调用class本身所有非静态的属性和非静态方法)的本身!