11

In standard OO, as defined by UML, and instantiated, e.g., by Java and C#, there is a certain concept of objects as instances of classes. What's the difference between this classical concept of objects and a JavaScript object?

0

2 Answers 2

17

JavaScript objects are different from classical OO/UML (C++/Java/C# etc.) objects. 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.

JavaScript objects can be used in many different ways for different purposes. Here are five different use cases for, or possible meanings of, JavaScript objects:

  1. A record is a set of property slots like, for instance,
    var myRecord = { firstName:"Tom", lastName:"Smith", age:26}
  1. 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 (e.g. it may contain blank spaces).

  1. 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); 
      }  
    };
  1. 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:
    var myApp = { model:{}, view:{}, ctrl:{} };
  1. A typed object o that instantiates a class defined by a JavaScript constructor function C is created with the expression
    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 Summary for more on JavaScript objects.

Sign up to request clarification or add additional context in comments.

6 Comments

Interesting. However I would suggest to remove UML from the comparison, as it appears that UML does not imply a strongly type language, even if it was designed with class based language in mind, and prototype based languages are not incompatible with UML. Consider in this regard the definition of a classifier: "A Classifier represents a classification of instances according to their Features". I don't think this implies that the class of an object is fixed. See also this question stackoverflow.com/q/69637013/3723423 - By the way, is your link still valid?
But the main issue here is not dynamic classification (which is neither supported in Java nor in JS), but the possibilities of objects (a) not instantiating any class, and (b) having method slots. [Doesn't the JavaScript Sumary link work for you?]
If an object shares no feature with any class, it's still an object. Then the prototyping creates implicitly some kind of classes, all this seems not incompatible with UML. - The link shows an almost empty page with a link to a document with a Russian title. So I wanted to be sure the page wasn't cyber-squatted in the meantime.
UML does not allow creating objects that do not instantiate a class (they say "A CreateObjectAction is an Action that creates a direct instance of a given Classifier"), while JS does allow this. No, the linked page is okay, it contains three links: (1) to an English HTML document, (2) to a Russian HTML document, and (3) to a PDF.
Interesting article with a lot of valuable information. I understand that instantiating an object that doesn't belong to any class in JS is anyhow creating an instance of Object, according to developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… : "Nearly all objects in JavaScript are instances of Object", which seems to make it compatible with the CreateObjectAction.
|
3

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). When i say mutable one can add some additional property to the Javascript object. 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

{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.

  1. Javascript Objects can be instantiated in many ways

Using Literals

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

Using Javascripts new Object

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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.