Question
JavaScript Problems #30 . Modify the Club function again: Add a property to this called add. The value of add will be a function. This
JavaScript Problems
#30 .
Modify the Club function again: Add a property to "this" called "add". The value of "add" will be a function. This function will take a name passed to it and add that name to the members list of the club. The function can't directly refer to whatever variable you store the new Club it must use this. The function should return the Club object when it is done. Finally, run this code to get the required output: var c = new Club("computer"); c.add("john"); c.add( "fred" ); console.log( c.members );
How do you assign a function to a variable? All functions are objects. You can refer to them by name, just don't put () on the right side of the name - because that will call the function. So you can copy function objects to anywhere you wish. You can also create a function without a name, called an anonymous function - in which case, you must assign it to a variable when you create it otherwise you'll never be able to use it (because it has no name to refer to.)
Club function - please use this and modify:
function Club(name) { this.name = name; this.members = []; }
var club = new Club('computer');
club.members.push('john'); club.members.push('fred');
console.log(club);
After Modify the Club function again, output:
#31.
If your add function / method works properly and returns it's object when done - you can now do something like this: console.log( (new Club("computer")).add("john").add("fred").members )
console.log) ["john", "fredStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started