Problem with static keyword of classes in js

Hi, i have a question , so the matter is that why when i use static at the end i have assign it to something then console log that think , because when i do it directly it will give me undefiend
for example =

  static pickSubstituteTeacher(SubstituteTeachers) {
    const random = Math.floor(Math.random() * SubstituteTeachers.length);
    return SubstituteTeachers[random];
  }

and for the output when i did this=

school.pickSubstituteTeacher([
  "Jamal Crawford",
  "Lou Williams",
  "J. R. Smith",
  "James Harden",
  "Jason Terry",
  "Manu Ginobli",
]);

console.log(PrimarySchool.SubstituteTeachers);

i got = undefined

but when i assigned it to something and then console logged it i got the output (i just hit the get unstuck button to see the answer so i have no idea why is it like this)

const sub = school.pickSubstituteTeacher([
  "Jamal Crawford",
  "Lou Williams",
  "J. R. Smith",
  "James Harden",
  "Jason Terry",
  "Manu Ginobli",
]);

console.log(sub);

i got the =Jamal Crawford as the output (I know it’s one of those names because i set it to be randomly given)

but why i had to do that to get the output why not just do it directly ?

First of all I’d like to say it’s great that Ginobili is becoming a substitute teacher.

But to be serious for half a moment. It’s a static method, meaning it exists outside the concept of any individual instantiations (considering it a global method is also valid enough)

So in the example of what your code is doing, when you write

school.pickSubstituteTeacher([
  "Jamal Crawford",
  "Lou Williams",
  "J. R. Smith",
  "James Harden",
  "Jason Terry",
  "Manu Ginobli",
]);
// this statement above returns one of those names,
// if you were to log it, it should log

Since it’s static, it’s not tied to any single instance, and so since the variable is not kept anywhere, that information is now essentially lost (although it was computed).

when you write

console.log(PrimarySchool.SubstituteTeachers);

without the (), you’re referring to the function itself instead of invoking it. So you’re saying, “log for me the function object” and not “log for me the result of invoking this function with some arguments”.

so

console.log(school.pickSubstituteTeacher([
  "Jamal Crawford",
  "Lou Williams",
  "J. R. Smith",
  "James Harden",
  "Jason Terry",
  "Manu Ginobli",
]);

will log what the function computed accordingly.

What’s important to note is that nothing is getting set by the function. It’s picking randomly from a given list regardless of what the school object is. You write school.foo() but foo is not tied to the specific instance of a school.

So to be clear

class Greeting {
    static sayHello() {
        console.log("Hello, World!");
    }
}

// i don't need to say, 
//myGreeting = new Greeting();
//myGreeting.sayHello()
Greeting.sayHello();

I say this because it’s unclear from the answer given that this was the case.

I imagine the rest of the code is

class School
{/*defined here*/}

school = new School();

const sub = school.pickSubstituteTeacher([
  "Jamal Crawford",
  "Lou Williams",
  "J. R. Smith",
  "James Harden",
  "Jason Terry",
  "Manu Ginobli",
]);

console.log(sub);

which is not how a static method would normally be thought of and used.

What would be normal would be

class School
{/*defined here*/}

const teacherPool = [
  "Jamal Crawford",
  "Lou Williams",
  "J. R. Smith",
  "James Harden",
  "Jason Terry",
  "Manu Ginobli",
]);

console.log(School.pickSubstituteTeacher(teacherPool));
2 Likes

but even with () it wont work because TypeError: PrimarySchool.SubstituteTeachers is not a function

here is what i undrestood =
since static is a general (utility), and global like you said =

and

so i have 2 options , 1 is to either tie it to something so it don’t get lost , like the solution=

or

or literally console log it

if I’m wrong please correct me

but even with () it wont work because TypeError: PrimarySchool.SubstituteTeachers is not a functio

you need to pass it an argument, when it says it’s not a function it’s because it’s looking for a function with that name that takes no arguments

here is what i undrestood =
since static is a general (utility), and global like you said =

correct

you’re correct on all fronts

no wait i replied to your first message and not the 4th edited variant of it .
i think im wrong , what did you meant here =

which is not the way to do it (even if it “works”).
The issue is that static would not be commonly used that way and this would create confusion. If it’s used this way, it doesn’t need to be static

actually you are right , and i also found this better =

because , we create a variable but just as a viriable like here =

and not assigning it to the =

and then on the calling , we call that on the variable , instead of assigning it to the variable directly and then log the variable . (i hope you undrestand what im saying :sweat_smile:)
so when we do it we maybe can use teacherPool later for something else but when we assing it directly to the .pickSubstituteTeacher we can’t .
right ?

because we are directly using it

problem is already solved but =

i did it and it didn`t worked =

console.log(PrimarySchool.SubstituteTeachers("Lou Williams"));

and then i get =

console.log(PrimarySchool.SubstituteTeachers("Lou Williams"));
                          ^

TypeError: PrimarySchool.SubstituteTeachers is not a function
console.log(PrimarySchool.pickSubstituteTeacher(["Lou Williams"]));

but it will only log him (yes its because we only passed him as an argument)
but i wanted to be a random selection (which i think this code will not to that)

am i right ?

If you put an fuller array it will do what your function does (pick random name). In fact, it randomly is picking Lou Williams right now, just trivially it’s picking randomly from a pool of one

console.log(PrimarySchool.pickSubstituteTeacher(["Lou Williams", "Tony Parker", "Tony Kukoc"]));

will pick randomly from these

1 Like

but if do this =

console.log(PrimarySchool.pickSubstituteTeacher(["Lou Williams", "Tony Parker", "Tony Kukoc"]));

the static method becomes useless!?

no wait it wont , we are just directly putting the array in the console log instead of making a seperate variable and then logging it like here =

No, the point about it being useful or not is this:

Does the user of this class need the method to be tied to a particular instance of the class or not?
Let’s say there’s many primary schools,
PS 3, PS 41, PS 115.

Does it make sense to say

ps3.pickSubstituteTeacher()
ps41.pickSubstituteTeacher()
ps115.pickSubstituteTeacher()

or does it make sense to say

PrimarySchool.pickSubstituteTeacher()

if it’s that latter, then you want a static method. If it’s the former, you don’t want a static method.

what ? ( i didn’t got it)

edit number 2 = i got it thanks

Does it make sense to say

A:

ps3.pickSubstituteTeacher()
ps41.pickSubstituteTeacher()
ps115.pickSubstituteTeacher()

or does it make sense to say

B:

PrimarySchool.pickSubstituteTeacher()

if it’s B, then you want a static method. If it’s A, you don’t want a static method.

1 Like

And again, that’s maybe not important for the exercise, but in real code this would be what you’re thinking about.

thanks
(I’m just writing this because it wont let me send something less that 20 word)

actually i just do the exercises to get the concept and always do extra things outside of the exercise because i dont care about the exercise i just care to learn the thing

2 Likes