【问题标题】:Java has Object class. What class is there in Javascript for similar purpose?Java有对象类。 Javascript中有什么类用于类似目的?
【发布时间】:2016-07-02 10:04:26
【问题描述】:

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

【问题讨论】:

  • Javascript 变量可以保存任何数据类型,包括数组、对象、布尔值甚至函数。
  • 在 JS 中变量没有类型(值有),所以你不需要 interface,因为 Java 有它们。您可以在运行时提供任何这些对象并使用它们的属性。

标签: javascript oop


【解决方案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());

【讨论】:

    【解决方案2】:

    您不需要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
    }
    

    查看此示例变量可以保存任何数据类型。

    【讨论】:

      【解决方案3】:

      如果你来自 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]; 
          } 
      }
      

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          猜你喜欢
          • 2015-01-03
          • 1970-01-01
          • 1970-01-01
          • 2015-05-20
          • 2014-04-30
          • 2018-09-30
          • 1970-01-01
          • 2020-02-06
          相关资源
          最近更新 更多