Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between creating a class in javascript to create an object and creating an class and object in Java

Statement:

Javascript can create an Object without creating a class.

Is that statement valid? Maybe I didn't get the concept here right...

For example if I create an object in Javascript:

var mercedes = new Object();
mercedes.speed = "260";
mercedes.model = "SLS";
mercedes.year = 1969;

Here I have no class but I already have an instance. Does that mean I don't need to define classes in Javascript?

Also one more question what is the difference in Javascript between creating a object as described above and creating an object in the following method:

myCar["speed"] = "260";
myCar["model"] = "SLS";
myCar["year"] = 1969;
like image 592
Nant Avatar asked Jun 04 '26 01:06

Nant


2 Answers

In javascript there are no classes. That being said you can simulate classes using few different ways.

Using a function and defining methods to its prototype

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

// you can define Person.fullname = function() {} straight to the function
// drawback to this is fullname function will be created everytimg you create a new Person
// instead you can create fullname in the prototype of the Person so it only gets created once 

Person.prototype.fullname = function() {
    return this.firstName + ' ' + this.lastName;
};

var person = new Person('John', 'Doe');
person.fullname(); // John Doe

Using object literals

var Person = {
    firstName: 'John',
  lastName: 'Doe',
  fullname: function() {
    return this.firstName + ' ' + this.lastName;
  }
};

Person.fullname(); // John Doe

Person.firstName = 'Jane';
Person.fullname(); // Jane Doe

Using Singleton

var person = new function() {
    this.firstName = 'John';
    this.lastName = 'Doe';
    this.fullname = function() {
      return this.firstName + ' ' + this.lastName;
    };
}; 

person.fullname(); // John Doe

person.firstName = 'Jane';
person.fullname(); // Jane Doe

If you are planning to get more closer to how java classes work, apply inheritance, etc then I suggest you to go with the first method. Below is example of a simple inheritance using prototype.

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

Person.prototype.fullname = function() {
  return this.firstName + ' ' + this.lastName;
};

Person.prototype.getFirstName = function() {
    return this.firstName;
};

Person.prototype.getLastName = function() {
    return this.lastName;
};

Person.prototype.setLastName = function(lastName) {
    return this.lastName = lastName;
};

Person.prototype.setFirstName = function(firstName) {
    return this.firstName = firstName;
};

var Student = function(firstName, lastName, grade) {
    this.grade = grade;
  Person.call(this, firstName, lastName);
};

Student.prototype = Object.create(Person.prototype);

Student.prototype.getGrade = function() {
    return this.grade;
};

var student = new Student('John', 'Doe', '5th');
student.fullname(); // John Doe
student.getFirstName(); // John
student.setFirstName('Jane');
student.getFirstName(); // Jane
student.getGrade(); // 5th

That should explain how you can use classes in javascript similar to you are used to in java.

What is difference between using dot notation and brackets to access object properties?

Long answer short, both behave exactly the same. But there would be instances where dot notation will not always work. For example:

var person = {
    "first name": "john",
    "last name": "doe",
    "fullname": "john doe"
};

person.fullname; // john doe

// to access first name property of person
person.first name; // will not work
person["first name"]; // john
like image 195
Subash Avatar answered Jun 06 '26 16:06

Subash


There are classes in javascript. But you can also inherit an object properties using the .prototype property. This W3School's article can explain it better.

For your second question there's no real difference when you use brackets or dot properties. Accesing properties using brackets can be useful when you use a variable to access an object property. Example:

 var obj = {}:
 var val = 'value';
 obj[val] = 'new value';
 console.log(obj[val]); // Output: new value

The above can also work with:

var obj = { 'value' = 'new value'};
var val = 'value';
console.log(obj[val]); // Output: new value
like image 42
Fernando Salas Avatar answered Jun 06 '26 16:06

Fernando Salas