2016-07-02 6 views
-3

ここにシナリオがあります。私はビジネスとビジネスという名前のクラスを持っています。ビジネスは、食品、エンターテイメントなどのドメインを持っています。ビジネスクラスはID、名前、ドメインへの参照などのプロパティを持っています。私は、食べ物、娯楽、衣類などの異なるドメインクラスを作成します。各ドメインクラスは独自のプロパティを持ちます(ほとんど共通することはありません)。 Javaの場合は、Objectクラスやインタフェースを使用するだけで、実行時にこれらの型の参照を提供することができました。 しかし、私はこれをJavascriptでどのように達成できるのか分かりません。この点については、どんな助力にも感謝します。JavaにはObjectクラスがあります。 Javascriptにはどのようなクラスがありますか?

+0

Javascript変数は、配列、オブジェクト、ブール値、さらには関数であっても任意のデータ型を保持できます。 – piechuckerr

+2

JS変数には型(値はありません)がありません。そのため、Javaが持つ意味では 'interface'は必要ありません。実行時にそれらのオブジェクトのいずれかを提供し、それらのプロパティを使用することができます。 – nnnnnn

答えて

1

これらのクラスのインスタンスを参照する変数にどのような型を使用するのかは、答えは「なし」です.JavaScriptは緩やかに型付けされた言語です。あなただけの変数/関数の引数/プロパティが含まれているものを使用:

function Foo() { 
 
    this.name = "foo"; 
 
} 
 

 
function Bar() { 
 
    this.name = "bar"; 
 
} 
 

 
function Bingo() { 
 
    this.name = "bingo"; 
 
} 
 

 
function use(x) { // <== No type on `x` 
 
    console.log(x.name); 
 
} 
 

 
use(new Foo()); 
 
use(new Bar()); 
 
use(new Bingo());

またはES2015 +

class Foo { 
 
    constructor() { 
 
     this.name = "foo"; 
 
    } 
 
} 
 

 
class Bar { 
 
    constructor() { 
 
     this.name = "bar"; 
 
    } 
 
} 
 

 
class Bingo { 
 
    constructor() { 
 
     this.name = "bingo"; 
 
    } 
 
} 
 

 
function use(x) { // <== No type on `x` 
 
    console.log(x.name); 
 
} 
 

 
use(new Foo()); 
 
use(new Bar()); 
 
use(new Bingo());

0

とあなたは別のタイプを必要としません変数の値はJavascriptです。 JavascriptはVaribleは、任意のデータ型を保持することができる。この例をチェック

var a = 10; 
var b = "PieChuckerr Here!" 
var isMale = true; 
var countriesLivedIn = ['US', 'UK', 10, 12]; // country_code as well as name 
var doCall = function(){} 
var obj = { 
    eyecolor: "blue", 
    age: 30 
} 

Java(そして素晴らしい)に比べて非常に異なっています。

0

あなたは、Javaから来た場合は、ここではJavaからJavaScriptの

への移行のexempleは、Javaコードです:

public class Cellule { 

    // properties 
    private String name = ""; 
    private Owner owner = null; 

    // constructor 
    public Cellule(String name){ 
     this.setName(name); 
    } 

    // méthods --> setter and getter 
    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public Owner getOwner() { 
     return owner; 
    } 

    public void setOwner(Owner owner) { 
     this.owner = owner; 
    } 
} 

public class Owner 
{ 
    // properties 
    private ArrayList<Cellule> cellules; 

    // constructor 
    public Owner(){ 
     this.cellules = new ArrayList<Cellule>(); 
    } 

    // methods 
    public int getSize(){ 
     return this.cellules.size(); 
    } 

    public int addElement(Cellule e){ 
     if(e.getOwner() != this) 
      e.setOwner(this); 
     this.cellules.add(e); 
     return this.cellules.indexOf(e); 
    } 

    public Cellule getElementAtIndex(int index){ 
     return this.cellules.get(index); 
    } 
} 

と同じことを行うためのJavaScript(ES6)コード:

class Cellule { 
    constructor(name){ 
     // properties 
     this.name = name; 
     this.owner = null; 
    } 
} 

class Owner{ 
    constructor(){ 
     // properties 
     this.cellules = []; 
    } 

    // methods 
    getSize(){ 
     return this.cellules.length; 
    } 

    addElement(cellule){ 
     if(cellule.owner != this) 
      cellule.owner = this; 
     return this.cellules.push(cellule); 
    } 

    getElementAtIndex(index){ 
     return this.cellules[index]; 
    } 
} 
0

プライベートメンバー(プロパティまたはメソッド)をES6構文で作成することはできませんが、次のようにES5構文で行うことができます。

var MyClass = function() { 
    var _name = "John"; // private property 

    this.getName = function() { // public method 
     return _name; // which returns the private property 
    } 
}; 
関連する問題