I have such a design in PHP (similar to Eloquent ORM):
class User {
private $id;
private $name;
public __constructor($id, $name) {
$this->id = $id;
$this->name = $name;
}
public function getName() {
return $this->name;
}
static function getUser($id) {
return new User($id, 'Adam');
}
}
I use it as follows:
$user = User::getUser(1);
Now I want to do this in Javascript. I got to this:
var User = function(id, name) {
this.id = id;
this.name = name;
}
User.prototype.getName = function() {
return this.name;
}
How to add a static function?
How do I call it so that it returns an instance of the object?
Does this design pattern have a name?
UPDATE:
Short answer to my question:
User.getUser = function(id) {
return new User(id, 'Adam');
}
source
share