So, let's say I have two classes, each of which requires the other:
Department.js:
const Person = require("./Person");
class Department{
constructor(id){
this.personel = getPersonel(id).map(person => new Person(person));
}
}
Person.js
const Department = require("./Department");
class Person {
constructor(id){
this.department = new Department(getDeptOfPerson(id));
}
}
const person = new Person(1);
const coworkers = person.department.personel;
Now, this doesn't work, and I can kinda understand why. It says "Person" is not a constructor at Department.js. However, if I put both classes in the same file, it works just fine.
So; my question is, how do I work around this? I'd really rather not keep both of these classes in the same file -- is there a better way?
I'm running the latest version of Node.
You can separate initializing objects from fetching data:
// types/Department.js
class Department {
constructor(id, personnel) {
this.id = id;
this.personnel = personnel;
}
}
// types/Person.js
class Person {
constructor(id, department) {
this.id = id;
this.department = department;
}
}
// findDepartment.js
const Department = require('./types/Department');
const Person = require('./types/Person');
function findDepartment(id) {
const personnel = getPersonnel(id).map(person => new Person(person));
return new Department(id, personnel);
}
// findPerson.js
const Department = require('./types/Department');
const Person = require('./types/Person');
function findPerson(id) {
const department = getDeptOfPerson(id);
return new Person(id, department);
}
These can even go back on the types (findDepartment.js → Department.js, Department.findDepartment = function (id) { …) if you really want.
Related
How can I create a class where each instantiation of the class auto-increments an ID?
I have a solution that relies on an IIFE and a closure over an 'id' that auto-increments. Is there a better way though?
The only alternative I can think of is another class PersonFactory that tracks the UID and has a method for creating Person with its own auto-increment UID.
class Person {
constructor(id, name){
this.id = id;
this.name = name;
}
}
// A factory that auto-increments the id given to each person
const PersonFactory = (function(){
let id = 0;
return (name) => {
id++;
return new Person(id, name);
}
})();
const jenny = PersonFactory('Jenny'); // ID: 1
const eliza = PersonFactory('Eliza'); // ID: 2
(Posted too soon)
The answer is static variables in your class!
Now you don't need a loosely related object to track state outside of an instantiation.
class Person {
static #id = 0;
static #incrementID() {
this.#id++;
}
constructor(name){
Person.#incrementID();
this.id = Person.#id;
this.name = name;
}
}
const jenny = new Person('Jenny'); // ID: 1
const eliza = new Person('Eliza'); // ID: 2
console.log(jenny.id)
console.log(eliza.id)
I want to access Main class methods to another Person class without creating a new instance Is it possible??
Can we access it without creating an instance of a class
let myInstance = new Person();
class Main {
constructor(args) {
this.hooks = [];
}
add_hooks(name, func) {
if (!this.hooks[name]) this.hooks[name] = [];
this.hooks[name].push(func);
}
call_hooks(name, ...params) {
if (this.hooks[name]) this.hooks[name].forEach((func) => func(...params));
}
}
other class Person how to access without using new keyword
const Main = require("./main.js");
class Person {
exec() {
const action = Main();
action.add_hook("jump", console.log.bind(console, "this will log "));
}
}
There is no big magic to it. Since the OP just wants to reuse prototypal Main methods, one is going to explicitly delegate the method/s of interest which was/were provided/accessed before via Main.prototype ...
class Main {
constructor(args) {
this.hooks = {};
}
add_hooks(name, func) {
if (!this.hooks[name]) {
this.hooks[name] = [];
}
this.hooks[name].push(func);
}
call_hooks(name, ...params) {
if (this.hooks[name]) {
this.hooks[name].forEach(func => func(...params));
}
}
}
// const Main = require("./main.js");
class Person {
// // ... either add `hooks` as public property at instantiation time ...
// hooks = {};
exec() {
const ref = Main.prototype;
ref.add_hooks.call(this, "jump", console.log.bind(console, "this will log"));
}
}
// ... or add `hooks` via additional glue code ...
function createPersonWithHooksAndExecute() {
const type = new Person();
type.hooks = {};
type.exec();
return type;
}
const someone = createPersonWithHooksAndExecute();
console.log({ someone });
// this will log
Main.prototype.call_hooks.call(someone, "jump");
.as-console-wrapper { min-height: 100%!important; top: 0; }
If you're not planning on instantiating the object, and you don't care about having multiple instances with each having their own state, you don't need a class.
Just create individual functions, or export an object.
const hooks = [];
export function add_hooks(name, func) {
if (!hooks[name]) hooks[name] = [];
hooks[name].push(func);
}
export function call_hooks(name, ...params) {
if (!hooks[name]) return;
for (const func of this.hooks[name]) {
func(...params);
}
}
It's possible too to do this with static methods, and that would be the likely answer if you write Java where everything has to be a class, but I wouldn't recommended it in Javascript.
According to second answer from here, I'm trying to create Singleton pattern in JS for storing data and invoking its prototypes from the other instances.
A main problem is Singleton doesn't store the data after receives the first instance.
[{…}]
0: {firstName: "John", lastName: "Grand"}
This is how I've done:
export default class Terminal {
static cache(output) {
// Singleton
if (!Terminal.instance) {
Terminal.instance = new Terminal(output);
}
return Terminal.instance;
}
constructor(output) {
// Create an array
this.logs = [];
// Switch to an object
const data = Object.assign({}, output);
// Add the object to the array
this.logs.push(data);
// Inspect
console.log(this.logs);
}
}
// instance 1
import Terminal from './terminal.js';
class Person {
constructor(firstName, lastName, input) {
this.firstName = firstName;
this.lastName = lastName;
// Assign the Singleton
this.input = input || Terminal.cache(this);
}
}
let player1 = new Person('John', 'Grand');
// instance 2
import Terminal from './terminal.js';
class Grocery {
constructor(name, stock, input) {
this.name = name;
this.stock = stock;
// Assign the Singleton
this.input = input || Terminal.cache(this);
}
}
let shop1 = new Grocery('Apple', 12);
I want to let the new keyword inside of the class when I define the Singleton pattern.
Any tips to accomplish my problem?
Thanks.
The cache() method needs to push output onto the logs array when the object already exists.
static cache(output) {
// Singleton
if (!Terminal.instance) {
Terminal.instance = new Terminal(output);
} else {
Terminal.instance.logs.push(Object.assign({}, output));
}
return Terminal.instance;
}
I have a class called Country with 4 constructor parameters.
I then create some new countries from that class with specified values.
My question is, how can i create a method that can find and return the object with a this.value equal to the input in the method?
class Country {
constructor(name, area, population, topdomain) {
this.name = name;
this.area = area;
this.population = population;
this.topdomain = topdomain;
}
static findCountry = domain => {
/*Here is where the magic should happen.
If domain is in any of the countries below, then it should return the country name.
*/
}
}
norway = new Country("Norway", 323802, 5320045, ".no");
sweden = new Country("Sweden", 450295, 9960487, ".se");
russia = new Country("Russia", 17098242, 142257519, ".ru");
china = new Country("China", 9596960, 1379302771, ".cn");
This function here should return "Norway":
Country.findCountry(".no");
For that to work, the class has to keep a list of all the created instances. As JS has no weak references, this will mean that none of the instances can ever be garbage collected (so be careful):
static instances = [];
constructor(/*...*/) {
/*...*/
Country.instances.push(this);
}
static findCountry = domain => {
return this.instances.find(country => country.domain === domain);
}
Don't come here to ask people to write your code ;)
class Country {
constructor(name, area, population, topdomain) {
this.name = name;
this.area = area;
this.population = population;
this.topdomain = topdomain;
Country._ALL.push(this);
}
static findBy(key, value) {
let output = [];
for ( let i in Country._ALL) {
let c = Country._ALL[i];
if (c.hasOwnProperty(key) && c[key] === value)
output.push(c);
}
return output;
}
}
Country._ALL = [];
WARNING! ES6 classes do not support static variables like static variable = [];
If you want static class variables in ES6 you have to use ClassName.variable = []; after the class declaration.
Your class doesn't know about the 4 objects you instantiated somewhere. You need to put them in a collection (e.g. an array), and then explicitly reference that collection in your search method:
class Country {
constructor(name, area, population, topdomain) {
this.name = name;
this.area = area;
this.population = population;
this.topdomain = topdomain;
}
static findCountry(domain) {
return (knownCountries.find(country => country.topdomain == domain) || {}).name;
// ^^^^^^^^^^^^^^
}
}
const norway = new Country("Norway", 323802, 5320045, ".no");
const sweden = new Country("Sweden", 450295, 9960487, ".se");
const russia = new Country("Russia", 17098242, 142257519, ".ru");
const china = new Country("China", 9596960, 1379302771, ".cn");
const knownCountries = [norway, sweden, russia, china];
// ^^^^^^^^^^^^^^
I'm kinda new with js + ES6 + class; I have problem with creating function inside constructor.
#1. I need to add new Hobby, a person allowed to have plenty hobbies ;
#2. I don't know how to show all the data of students;
another questions are in the comments ,in case if you want to answer it too, if not i'm also fine.
so here's my code :
class Student {
constructor(name,hobbies){
this.name = name;
var hobby = new Set(); //do I set here or inside the function ??
//since the function addHobbies also need, then it's fine to be global right ?
this.hobbies = (hobbies) => { //function ES6 like this right ??
this.hobbies = hobby.add(hobbies);
return this.hobbies; //can I return hobby instead of this.hobbies ??
};
}
setName(newName){
this.name = newName;
}
addHobbies(newHobbies){
this.Hobbies = hobby.add(newHobbies); //it should be like this to add >> to set ?
}
getName(){
return this.name;
}
getHobbies(){
return this.hobbies;
}
}
and how to return all the data ?
let andy = new Student("andy","dance");
let vince = new Student("vince","codding");
so it will show all students-attribute by getCode() ?
do I set here or inside the function ??
That depends on what you need. Do you want each Student instead to have one set of hobbies, or do you want to create a new set every time the function is called?
this.hobbies = (hobbies) => { //function ES6 like this right ??
this.hobbies = hobby.add(hobbies);
That doesn't work at all. You're creating the property with a function value, but when the method is called you're overwriting the property with the return value of the add method.
To make it work, I'd recommend making the .hobbies set an instance property instead of a local variable.
class Student {
constructor(name, ...hobbies) {
this.name = name;
this.hobbies = new Set();
this.addHobbies(...hobbies);
}
getName() {
return this.name;
}
setName(newName) {
this.name = newName;
}
getHobbies() {
return this.hobbies;
}
addHobbies(...newHobbies) {
for (const newHobby of newHobbies)
this.hobbies.add(newHobby);
}
}
Alternatively, if you insist on using a local constructor variable, it would look like this:
class Student {
constructor(name, ...hobbies) {
this.name = name;
this.hobbies = new Set(...hobbies);
this.getHobbies = () => {
return this.hobbies;
};
this.addHobbies = (...newHobbies) => {
for (const newHobby of newHobbies)
this.hobbies.add(newHobby);
};
}
… // further methods (for name etc)
}
Try this:
class Student {
constructor(name, hobbies) {
this.name = name;
// Allow passing both an array of hobbies and a single hobby
this.hobbies = Array.isArray(hobbies) ? new Set(hobbies) : new Set([hobbies]);
}
setName(newName) {
this.name = newName;
}
addHobbies(newHobbies) {
if (Array.isArray(newHobbies)) {
newHobbies.forEach((hobby) => this.hobbies.add(hobby));
} else {
this.hobbies.add(newHobbies);
}
}
getName() {
return this.name;
}
getHobbies() {
return this.hobbies;
}
}
let andy = new Student("andy","dancing");
let vince = new Student("vince",["codding", "running"]);
andy.addHobbies("slipping");
vince.addHobbies(["running", "eating"]);
You are in the correct direction. I have rewritten your class to do what I think is more similar to what you are trying to achieve.
Play with the code at: https://jsbin.com/vejumo/edit?js,console
And here's the rewritten class:
class Student {
constructor(name, hobbies = []){
this.name = name;
// new Set() is used to work with objects. It does not work with well with strings
// Let's use an array to store the hobbies.
// if a hobby or an hobbies array is passed, store it, otherwise set an empty array.
this.hobbies = this.parseHobbies(hobbies);
}
// This function will normalize the hobbies to an Array
parseHobbies(hobbies) {
if (typeof hobbies === "string") {
// hobbies is a string, means it's a single hobby and not an array
return [hobbies];
}
// Assuming the hobbies is a an Array
return hobbies;
}
setName(newName) {
this.name = newName;
}
// this function will allow you to add a single hobby to the array
addHobbies(hobbies = []) {
// Same logic like in constract, this can accept a string or an array
// We use Array.concat and push to append to array
this.hobbies = this.hobbies.concat(this.parseHobbies(hobbies));
}
getName() {
return this.name;
}
getHobbies() {
return this.hobbies
}
// This will return all student attributes.
getAttributes() {
// Return a copy of all the attributes instead of returning references
return Object.assign({}, this);
}
}
let george = new Student("George", "Sports");
george.addHobbies(["Singing", "Fishing"]);
george.addHobbies("Dancing");
console.log(george.getAttributes());