Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 295
Gerd Wagner Avatar asked Feb 24 '14 22:02

Gerd Wagner


People also ask

What is difference between Java object and JavaScript object?

Java is an object-oriented programming language. JavaScript is an object-based scripting language. Java applications can run in any virtual machine(JVM) or browser. JavaScript code used to run only in the browser, but now it can run on the server via Node.

What is a JavaScript object in Java?

In JavaScript there are "object" data types where a given variable essentially has sub-variables with their own unique values, for example: var car = {type:"Fiat", model:500, color:"white"}; It is almost like an array, but not quite (JavaScript has arrays too).

What is the difference between JavaScript object and class?

A class defines object properties including a valid range of values, and a default value. A class also describes object behavior. An object is a member or an "instance" of a class. An object has a state in which all of its properties have values that you either explicitly define or that are defined by default settings.

How are JavaScript objects different?

JavaScript Objects are Mutable Objects are mutable: They are addressed by reference, not by value. If person is an object, the following statement will not create a copy of person: const x = person; // Will not create a copy of person. The object x is not a copy of person.


2 Answers

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}
    
  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 (e.g. it may contain blank spaces).

  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:

    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

    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.

like image 145
Gerd Wagner Avatar answered Oct 16 '22 22:10

Gerd Wagner


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");
like image 33
raj240 Avatar answered Oct 16 '22 22:10

raj240