1
我試圖將我的一些JavaScript舊代碼轉換爲顯示原型模式。我寫了下面的代碼,但得到錯誤RevealingPrototypePattern:TypeError:....不是構造函數
TypeError: AllShowing is not a constructor
$(document).ready(function() {
var allShowing = new AllShowing();
// Edit record
$('#allShowingGrid').on('click', 'a.edit_showing', function (e) {
e.preventDefault();
$('html, body').animate({ scrollTop: 0 }, 'fast');
var command = 'Edit';
var tr = $(this).closest('tr');
allShowing.mapGridDataToFormData(command, tr);
});
});
var AllShowing = function() {
this.gridContainer = "#allShowingGrid";
this.showRequestData = function() {
$("#requestData").show();
};
};
AllShowing.prototype = function() {
var mapGridDataToFormData = function(command, tr) {
if (command == 'Edit') {
showRequestData.call(this);
var id = $.trim((tr).children("td.row_id").text());
var propId = $.trim((tr).children("td.showing_PropertyId").text());
var name = $.trim((tr).children("td.showing_FirstName").text());
var email = $.trim((tr).children("td.showing_Email").text());
var phone = $.trim((tr).children("td.showing_WorkPhone").text());
var schedule = $.trim((tr).children("td.showing_PreferredDate").text());
var reqDetails = $.trim((tr).children("td.showing_Comment").text());
var startHour = $.trim((tr).children("td.showing_StartHour").text());
$("#hdVisitorId").val(id);
$("#FirstName").val(name);
$("#Email").val(email);
$("#Work_Phone").val(phone);
$("#PreferredDate").val(schedule);
$("#StartHour").val(startHour);
}
};
return {
mapGridDataToFormData: mapGridDataToFormData
};
}();
我查了一些類似的帖子像Javascript: TypeError: … is not a constructor和Javascript 「Not a Constructor」 Exception while creating objects
但不能解決我的錯誤。任何機構都可以幫助我,我犯了一個錯誤。
至少,你應該使用正確的語法構造:'函數AllShowing()'。 – Andy
這是因爲當你試圖調用它時'allShowing'是'undefined'。相關:http://stackoverflow.com/q/336859/1169519 – Teemu
@Teemu謝謝了。 – Arif