DEV Community

Discussion on: How Do Objects Work In JavaScript?

Collapse
 
jazkh profile image
jazkh • Edited

JS constructor would be more useful.

function User(username, password, email) {
    this.username = username
    this.password = password
    this.email = email
    this.isActive = function() {
        return (this.password ? "Y" : "N");
    }
} 

Then you can assign it as much as you want:

var student1 = new User('alex', 'p@ssw0rd', 'alex@example.com')

var student2 = new User('john', 'kRypt0n', 'john@example.com')


student1.isActive()

And you can change property value on the fly:

student1.password = 'myNewP@$$w0rd'
Collapse
 
gmartigny profile image
Guillaume Martigny

It's even more slick with ES6 classes:

class User {
  constructor (username, password, email) {
    this.username = username;
    this.password = password;
    this.email = email;
  }

  isActive () {
    return Boolean(this.password);
  }
}