使用class封装乘法表
function Cfb(x){
this.init(x);
}
//初始化方法,当咱们构造函数上的方法比较多的时候
//可以先把方法在它里面调用然后直接在构造函数内部调用init方法即可
//归纳
Cfb.prototype.init = function(x){
this.calc(x);
}
Cfb.prototype.calc = function(x){
let str = '<table>';
for(let i=1; i<=x; i++){
str += '<tr>';
for(let j=1; j<=i; j++){
str += `<td style='border:1px solid red'>${j}×${i}=${j*i}</td>`;
}
str += '</tr>';
}
str += '</table>';
document.write(str)
}
对封装好的类进行调用
示例一:生成9*9的乘法表
new Cfb(9)
示例一:生成11*11的乘法表
new Cfb(11)