简体   繁体   English

JavaScript 对象和 OO/UML/Java 对象有什么区别?

[英]What's the difference between a JavaScript object and an OO/UML/Java object?

In standard OO, as defined by UML, and instantiated, eg, by Java and C#, there is a certain concept of objects as instances of classes.在 UML 定义并实例化的标准 OO 中,例如由 Java 和 C# 实例化,存在将对象作为类实例的特定概念。 What's the difference between this classical concept of objects and a JavaScript object?这个经典的对象概念和 JavaScript 对象有什么区别?

JavaScript objects are different from classical OO/UML (C++/Java/C# etc.) objects. JavaScript 对象不同于经典的 OO/UML(C++/Java/C# 等)对象。 In particular, they need not instantiate a class .特别是,他们不需要实例化一个类 And they can have their own (instance-level) methods in the form of method slots, so they do not only have (ordinary) property slots , but also method slots .并且它们可以以方法槽的形式拥有自己的(实例级)方法,因此它们不仅有(普通)属性槽,还有方法槽 In addition they may also have key-value slots .此外,它们也可能有键值槽 So, they may have three different kinds of slots, while classical objects (called "instance specifications" in UML) only have property slots.因此,它们可能具有三种不同类型的槽,而经典对象(在 UML 中称为“实例规范”)只有属性槽。

JavaScript objects can be used in many different ways for different purposes. JavaScript 对象可以以多种不同的方式用于不同的目的。 Here are five different use cases for, or possible meanings of, JavaScript objects:以下是 JavaScript 对象的五种不同用例或可能的含义:

  1. A record is a set of property slots like, for instance,记录是一组属性槽,例如,

     var myRecord = { firstName:"Tom", lastName:"Smith", age:26}
  2. An associative array (or 'hash map') is a set of key-value slots.关联数组(或“哈希映射”)是一组键值槽。 It supports look-ups of values based on keys like, for instance,它支持中进行查找基于像钥匙,例如,

     var numeral2number = { "one":"1", "two":"2", "three":"3"}

    which associates the value "1" with the key "one", "2" with "two", etc. A key need not be a valid JavaScript identifier, but can be any kind of string (eg it may contain blank spaces).它将值“1”与键“一”、“2”与“二”等相关联。键不必是有效的 JavaScript 标识符,但可以是任何类型的字符串(例如,它可能包含空格)。

  3. An untyped object does not instantiate a class.无类型对象不会实例化类。 It may have property slots and method slots like, for instance,它可能有属性槽和方法槽,例如,

     var person1 = { lastName: "Smith", firstName: "Tom", getInitials: function () { return this.firstName.charAt(0) + this.lastName.charAt(0); } };
  4. A namespace may be defined in the form of an untyped object referenced by a global object variable, the name of which represents a namespace prefix.命名空间可以以由全局对象变量引用的无类型对象的形式定义,其名称代表命名空间前缀。 For instance, the following object variable provides the main namespace of an application based on the Model-View-Controller (MVC) architecture paradigm where we have three subnamespaces corresponding to the three parts of an MVC application:例如,以下对象变量提供了基于模型-视图-控制器 (MVC) 架构范式的应用程序的主命名空间,其中我们有三个子命名空间,对应于 MVC 应用程序的三个部分:

     var myApp = { model:{}, view:{}, ctrl:{} };
  5. A typed object o that instantiates a class defined by a JavaScript constructor function C is created with the expression实例化由 JavaScript 构造函数C定义的类的类型化对象o使用表达式创建

    var o = new C(...)

    The type/class of such a typed object can be retrieved with the introspective expression可以使用自省表达式检索此类类型对象的类型/类

    o.constructor.name // returns "C"

See my JavaScript Sumary for more on JavaScript objects.有关 JavaScript 对象的更多信息,请参阅我的JavaScript Sumary

Along with the above you can add the below mentioned point:除上述内容外,您还可以添加以下提到的点:

  1. Javascript Objects are mutable(we can add properties) where as Java Objects are immutable(we cannot add properties but we can change the value of the property by means of setters). Javascript 对象是可变的(我们可以添加属性),而 Java 对象是不可变的(我们不能添加属性,但我们可以通过 setter 来更改属性的值)。 When i say mutable one can add some additional property to the Javascript object.当我说可变时,可以向 Javascript 对象添加一些额外的属性。 Say for example.比如说。

    var person = { firstName: "John", secondName: "Deer", }

later on we can change it by adding additional properties.稍后我们可以通过添加其他属性来更改它。 Say

Person.age = 25;

after this step Person will change to在此步骤之后 Person 将更改为

{firstName: "John", secondName: "Deer", age: 25}

Where as this way of adding properties to the instantiated object is not possible in the case of Java.在 Java 的情况下,这种向实例化对象添加属性的方式是不可能的。

  1. Javascript Objects can be instantiated in many ways可以通过多种方式实例化 Javascript 对象

Using Literals使用文字

var person = {firstName:"Deen",lastName:"Deer"}

Using Javascripts new Object使用 Javascripts 新对象

var person = new Object();
person.firstName = "John";
erson.lastName = "Deer";

Using Function使用功能

function Person(firstName, lastName){
this.firstName = firstName;
this.lastName = lastName;
}

and you can create person object as并且您可以将人员对象创建为

var person = new Person("John","Deer");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM