简体   繁体   中英

Learning OOP Javascript, I'm trying really hard, but it isn't sinking in. Can anyone help me with this problem?

So, I started with this. I think, Worker is my class

    function Worker(firstName, lastName, role, action) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.role = role;
        this.action = action;
        Worker.random = function (name) {
            name = this.name
            let actions = this.action
            console.log(Math.floor((Math.random() * name.action)))
        }


    }

Now I'm trying to make a 'new' object out of it here,

let Stanley = new Worker('Stanley', 'Hudson', 'Sales', [
    'Did I Stutter?? You and Michael drink 3.',
    'You just had a heart attack and every time Michael opens his mouth you\'re stress levels to up. Drink 4 to calm your nerves',
    'Your cheating has caught up with you. Bad Stanley. Drink 2'
])

How do I make it so that I can Stanley.random() and get back a random action from the action array? I want to be able to use the random() function on any of the characters. I just don't really know how to go about it? This is the point right? to be able to use the methods over and over again?

I know I'm dumb, if you'd like to tell me that, feel free, just know that I know. Any help, is very appreciated

You can assign functions to class props as well as regular data.. therefore, you'll need to assign this.random to a function, instead of Worker.random .. Something like this should do the trick..

 function Worker(firstName, lastName, role, action) { this.firstName = firstName; this.lastName = lastName; this.role = role; this.action = action; this.random = function() { if (this.action.length > 0) { return this.action[Math.floor(Math.random() * this.action.length)]; } }; } let Stanley = new Worker("Stanley", "Hudson", "Sales", [ "Did I Stutter?? You and Michael drink 3.", "You just had a heart attack and every time Michael opens his mouth you're stress levels to up. Drink 4 to calm your nerves", "Your cheating has caught up with you. Bad Stanley. Drink 2" ]); let randomAction = Stanley.random(); console.log("Random Action From Stanley:", randomAction);

  1. Your function declaration is incorrect. It should be this.random = ... instead of Worker.random = ... .

  2. Your random function is incorrect. It should pick a random index based on the length of the actions array .

 function Worker(firstName, lastName, role, actions) { this.firstName = firstName; this.lastName = lastName; this.role = role; this.actions = actions; this.random = function() { console.log( this.actions[Math.floor(Math.random()*this.actions.length)] ); } } let Stanley = new Worker('Stanley', 'Hudson', 'Sales', [ 'Did I Stutter?? You and Michael drink 3.', 'You just had a heart attack and every time Michael opens his mouth you\\'re stress levels to up. Drink 4 to calm your nerves', 'Your cheating has caught up with you. Bad Stanley. Drink 2' ]); Stanley.random();

Alternatively, use a true class and define random as a prototype method :

 class Worker { constructor(firstName, lastName, role, actions) { this.firstName = firstName; this.lastName = lastName; this.role = role; this.actions = actions; } random() { console.log(this.actions[Math.floor(Math.random() * this.actions.length)]); } } let Stanley = new Worker('Stanley', 'Hudson', 'Sales', [ 'Did I Stutter?? You and Michael drink 3.', 'You just had a heart attack and every time Michael opens his mouth you\\'re stress levels to up. Drink 4 to calm your nerves', 'Your cheating has caught up with you. Bad Stanley. Drink 2' ]); Stanley.random();

You have two problems:

  1. you need to write a correct random() method
  2. you need to correctly put that method on your object or class.

You have two options to put the method on the object:

put the method on the object itself

You just need to replace Worker.random = function (name) { by this.random = function (name) { and you're done. However this means you will duplicate the method for each instance.

use the prototype

You can put a method on the prototype and it will be shadowed to all instances. So do this:

function Worker(firstName, lastName, role, action) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.role = role;
    this.action = action;
}

Worker.prototype.random = function (name){
  ...
}

now about your method, you probably want this:

function () {
  const index = Math.floor((Math.random() * this.actions.length));
  return this.actions[index];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM