Java有对象类.Javascript中有什么类可以达到类似的目的

Java has Object class. What class is there in Javascript for similar purpose?

本文关键字:什么 对象 Javascript Java      更新时间:2023-09-26

下面是一个场景。我有一个名为商业的课程,商业有一个像食品,娱乐等领域。业务类具有id、名称和对域的引用等属性。我将创建不同的领域类,如食品,娱乐和服装。每个域类都有自己的属性(可能很少)。如果是Java,我可以简单地使用Object类或一些允许我在运行时提供任何这些类型引用的接口。但我不知道如何在Javascript中实现这一点。

如果你的意思是你会为这些类的变量引用实例使用什么类型,答案是:没有,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());

Javascript中不需要单独的变量类型。JavascriptJava有很大的不同

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,这里有一个从Java迁移到JavaScript的示例

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

请注意,在ES6语法中不能创建私有成员(属性或方法),但在ES5语法中可以,如下所示:

var MyClass = function() {
    var _name = "John"; // private property
    this.getName = function() { // public method
        return _name; // which returns the private property
    }
};