3 ms·
sure :P (EDIT: Just kidding; the parent's point totally flew over my head). I've actually created classes this way in the past when I worked with a team of ente
by rmrfrmrf 11y ago
sure :P (EDIT: Just kidding; the parent's point totally flew over my head). I've actually created classes this way in the past when I worked with a team of enterprise Java developers lol. You could also make magic getter/setter methods if you really wanted to get crazy with it.
var Person = (function() {
return function(firstName, lastName) {
var privateVars = {
firstName: "",
lastName: ""
};
this.getFirstName = function() {
return privateVars.firstName;
};
this.setFirstName = function(firstName) {
privateVars.firstName = firstName;
return this;
};
this.getLastName = function() {
return privateVars.lastName;
}
this.setLastName = function(lastName) {
privateVars.lastName = lastName;
return this;
};
this.sameFirstName = function(otherPerson) {
return this.getFirstName() == otherPerson.getFirstName();
}
this.setFirstName(firstName);
this.setLastName(lastName);
Object.freeze(this);
return this;
}
}());
var p = new Person("John", "Doe");
var q = new Person("John", "Smith");
var r = new Person("Jane", "Doe");
p.sameFirstName(q); // true
p.sameFirstName(r); // false
- Cushman 11y agoThis isn't quite the same; the parent is pointing out that you can implement `sameFirstName` without exposing `firstName` publicly at all, not even as a getter.