我有一个包含更多函数的 javascript 代码。每个功能代码内部看起来相似。有什么方法可以减少和优化使用 javascript oop 的代码。所以我的脚本是这样的。
function cal_a() {
var a_list = [];
function fetch_dom() {
var a = document.getElementById("pOne");
a.innerHTML = "Hello";
a_list.push("Hello");
}
fetch_dom();
}
function cal_b() {
var b_list = [];
function fetch_dom() {
var b = document.getElementById("pTwo");
b.innerHTML = "World";
b_list.push("World");
}
fetch_dom();
}
cal_a();
cal_b();
//..
//..
//..
//cal_z();
HTML代码看起来
<p id="pOne"></p>
<p id="pTwo"></p>
如果问题有误请见谅。提前致谢。
最佳答案
我不得不说列表在这里没有做任何事情
function cal(id, text) {
var list = [];
function fetch_dom() {
var el = document.getElementById(id);
el.innerHTML = text;
list.push(text);
}
fetch_dom();
}
cal('id', 'text');
关于javascript - 使用 javascript oop 减少代码重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51185089/