Jump to content

I'm in the middle of an assignment for a JS program I'm working on. We are working on classes. Under what circumstances would you need to code the intern and manager objects as functions? Invoking that function doesn't seem to do anything.

 

I'm sorry if this is all dumb, I'm new to JS and coding!

Screenshot 2023-05-19 at 12.05.04 PM.png

Link to comment
https://linustechtips.com/topic/1508047-why-is-this-a-function/
Share on other sites

Link to post
Share on other sites

I assume they're used for demonstration purposes. They create specific Worker instances (an "intern" and a "manager"). The functions return these instances, so in order for them to do something useful, you'd need to call them somewhere else and do something with the returned Worker instances.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
https://linustechtips.com/topic/1508047-why-is-this-a-function/#findComment-15948297
Share on other sites

Link to post
Share on other sites

17 minutes ago, Eigenvektor said:

I assume they're used for demonstration purposes. They create specific Worker instances (an "intern" and a "manager"). The functions return these instances, so in order for them to do something useful, you'd need to call them somewhere else and do something with the returned Worker instances.

So if I call the manager function, it would return that specific manager Alice?

Link to comment
https://linustechtips.com/topic/1508047-why-is-this-a-function/#findComment-15948311
Share on other sites

Link to post
Share on other sites

44 minutes ago, hamiltonsandwich said:

So if I call the manager function, it would return that specific manager Alice?

Yes, manager() will return an instance of the superclass Person with handle = 'Alice', along with additional properties (xp, hourlyWage) afforded by the Worker subclass.

console.log(intern().xp);

Using the intern() method as shown, what do you think the value of the xp property will be?

Link to comment
https://linustechtips.com/topic/1508047-why-is-this-a-function/#findComment-15948357
Share on other sites

Link to post
Share on other sites

58 minutes ago, Vicarian said:

Yes, manager() will return an instance of the superclass Person with handle = 'Alice', along with additional properties (xp, hourlyWage) afforded by the Worker subclass.

console.log(intern().xp);

Using the intern() method as shown, what do you think the value of the xp property will be?

So, I guess my question then is, how is this: 

function intern() {
    var intern = new Worker('Bob', 21, 110, 0, 10);
    intern.goToWork();
    return intern;
}

console.log(intern().xp) // 10

different than this:

var intern = new Worker('Bob', 21, 110, 0, 10)
intern.goToWork();

console.log(intern.xp) // 10

?

Link to comment
https://linustechtips.com/topic/1508047-why-is-this-a-function/#findComment-15948432
Share on other sites

Link to post
Share on other sites

Functionally there is no difference in the result. In one case you are using a function to return the new instance of a worker in the other you are setting the new worker instance to the variable intern.

 

Using a function to return the worker instance would allow you to follow the DRY (Don't Repeat Yourself) principle, for example, you can supply arguments to intern() to change the name, age, and experience programmatically to add 6 interns instead of one.

Link to comment
https://linustechtips.com/topic/1508047-why-is-this-a-function/#findComment-15948473
Share on other sites

Link to post
Share on other sites

5 hours ago, D4NGRB0X said:

Functionally there is no difference in the result. In one case you are using a function to return the new instance of a worker in the other you are setting the new worker instance is being set to the variable intern.

 

Using a function to return the worker instance would allow you to follow the DRY (Don't Repeat Yourself) principle, for example, you can supply arguments to intern() to change the name, age, and experience programmatically to add 6 interns instead of one.

That makes sense, thank you!

Link to comment
https://linustechtips.com/topic/1508047-why-is-this-a-function/#findComment-15948687
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×