Java有对象类.Javascript中有什么类可以达到类似的目的
Java has Object class. What class is there in Javascript for similar purpose?
下面是一个场景。我有一个名为商业的课程,商业有一个像食品,娱乐等领域。业务类具有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
中不需要单独的变量类型。Javascript
和Java
有很大的不同
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
}
};
相关文章:
- 全局变量和全局对象的属性之间有什么区别吗
- 在Javascript中重新分配对象变量时,原始对象会发生什么
- 确定var是否是javascript中的elementFinder对象的方法是什么
- 在这个使用hasOwnProperty的对象扩展程序中有一个错误,I'我不确定那个bug是什么,也不确定这个扩展
- 用javascript修复这个JSON对象字符串最干净的方法是什么
- 从AngularJs获取谷歌地图对象的正确方法是什么
- 什么是“;原型;通过JavaScript中的Object Literal Notation创建的对象的链接
- 克服错误的更优雅的方法是什么:需要对象说明符.当通过JXA通过Messages发送SMS时,参数没有对象说明符
- XMLHttpRequest对象的open()和send()方法之间有什么区别
- 这两种不同的创建对象文字的方法有什么区别
- 什么'是从对象列表中一次编辑一个对象的正确Angular/Firebase方法
- 我需要使用什么语法来向一个对象的成员添加一个临时数组,该成员等同于一个字符串的通用列表
- 使用查询将对象数组发布到asp.net-mvc控制器操作的正确方法是什么
- 在jquery中,从同一对象的属性设置输入字段和标签的正确方法是什么
- 什么是'值是引用'表示JavaScript对象
- Google script/JavaScript:这在对象后面做什么:(对象名称||{});.
- 什么c对象被映射到js字典
- 这在jquery中意味着什么$对象
- Node.js在什么对象中存储变量
- . responsexml()返回什么对象