我想使用Table类,因为可以在JavaScript中轻松实现,如下面的代码片段所示。在声明了对象之后,我们可以使用new运算符实例化它并使用它的属性和方法。但是我的代码不起作用。请看一看。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>:Functions as Variables:</title>
<style>
table {margin-bottom:15px;}
td {padding:10px; text-align:center; border:1px solid #cecece;}
</style>
<script src="js/jquery-1.11.3.min.js"></script>
</head>
<body>
<table cellpadding="0" cellspacing="0" width="100%" border="0">
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>10</td>
</tr>
<tr>
<td>11</td>
<td>12</td>
</tr>
<tr>
<td>13</td>
<td>14</td>
</tr>
</table>
<script>
function table(row,column) {
this.row = row;
this.column = column;
getCountCell = function(){
return this.rows * this.column;
}
}
var c = new table(3,5);
var t = c.getCountCell();
console.log(t);
</script>
</body>
</html>
答案 0 :(得分:1)
您可以尝试此代码
(function(){
var rows;
var columns;
this.table = function table(row,column) {
this.rows = row;
this.columns = column;
}
table.prototype.getCountCell = function() {
return this.rows * this.columns;
}
})();
var c = new table(3,5);
var t = c.getCountCell();
console.log(t);
的详细信息
答案 1 :(得分:0)
拼写错误是row
而不是rows
并在this
前面应用this.getCountCell
function table(row,column) {
this.row = row;
this.column = column;
this.getCountCell = function(){
return this.row * this.column;
}
}
var c = new table(3,5);
var t = c.getCountCell();
console.log(t);
table {margin-bottom:15px;}
td {padding:10px; text-align:center; border:1px solid #cecece;}
<table cellpadding="0" cellspacing="0" width="100%" border="0">
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>10</td>
</tr>
<tr>
<td>11</td>
<td>12</td>
</tr>
<tr>
<td>13</td>
<td>14</td>
</tr>
</table>