Javascript 需要在Js中使用继承、封装和抽象
我试图在代码中使用继承、封装、抽象和对象概念。但是我不知道如何在下面的代码中使用它。如何将代码更改为新概念。我在下面的链接中添加了小提琴Javascript 需要在Js中使用继承、封装和抽象,javascript,oop,Javascript,Oop,我试图在代码中使用继承、封装、抽象和对象概念。但是我不知道如何在下面的代码中使用它。如何将代码更改为新概念。我在下面的链接中添加了小提琴 function createTable() { tableContainer.appendChild(table); if ((table.nodeValue) != 0) { table.innerHTML = ""; } var row = document.createElement('tr'); table.append
function createTable() {
tableContainer.appendChild(table);
if ((table.nodeValue) != 0) {
table.innerHTML = "";
}
var row = document.createElement('tr');
table.appendChild(row);
headcell = document.createElement('th');
row.appendChild(headcell);
headcell.innerHTML = "Select";
headcell = document.createElement('th');
row.appendChild(headcell);
headcell.innerHTML = "Sl.No";
Object.keys(obj[0]).forEach(function(val) {
headcell = document.createElement('th');
row.appendChild(headcell);
headcell.innerHTML = val;
});
headcell = document.createElement('th');
row.appendChild(headcell);
headcell.innerHTML = "Action";
}
您最好看看下一代Javascript:(以前是ES6)或者它是Javascript的超集。它们都集成了类的概念 限制是ES2015还没有得到浏览器的很好支持,所以两者都需要转换为经典Javascript(ES5) ES5中的经典方法是使用构造函数(按页面输入):
再看一看我不明白你想达到什么目的。你为什么要做OOP?你想把你的函数分成几个函数吗?在JS POO中,函数是构造函数。要添加方法,请修改原型。函数MyClass(){}MyClass.prototype.myMethod=function(){}我只想使用继承、多态性和封装概念。
//constructor method
function Apple (type) {
//"public" property
this.type = type;
this.color = "red";
//private variable, only visible in the scope of the constructor
logInfo(this.getInfo());
//public method
this.getInfo = function() {
return this.color + ' ' + this.type + ' apple';
};
//private method
function logInfo(info){
console.log(info);
}
}
//instancitation
var myApple = new Apple("macintosh");
//You can access public properties
myApple.color="red";
console.log(myApple.getInfo());