2016-04-14 7 views
-1

私のコードに継承、カプセル化、抽象化、およびオブジェクトの概念を使用しようとしています。しかし、私は次のコードでそれをどのように使うのか分かりません。私のコードを新しい概念に変更する方法。私は以下のリンクにフィドルを追加しました。継承、カプセル化、抽象化をJsで使用する必要があります

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"; 
} 

Jsfiddle

+0

を見てください。なぜあなたはOOPをやりたいのですか?関数をいくつかの関数に分割したいのですか? –

+0

JS POOでは、この関数はコンストラクタです。メソッドを追加するには、プロトタイプを変更します。 function MyClass(){} MyClass.prototype.myMethod = function(){} – Kulvar

+0

私はちょうど継承、多態性、カプセル化の概念を使いたいと思います。 –

答えて

1

あなたは多分、より良いのJavascriptの次の世代を見てしたい:ES2015(旧ES6)またはTypeScriptするためにJavaScriptのスーパーセットです。彼らは両方ともクラスの概念を統合しています。

ES2015はブラウザで十分サポートされていないため、両方を古典的なJavascript(ES5)に変換する必要があります。

ES5で、古典的な方法は、(thisページでinpired)コンストラクタを使用して行われます:

//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()); 

も、私はあなたが達成しようとしているのか理解していないthis answer

関連する問題