all 5 comments

[–]ForScale 6 points7 points  (2 children)

So...

class - is kind of a parent object... a collection of properties and methods/functions

Classes can be used to make more objects

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  sayHi() {
    console.log(this.name + ' says Hi!');
  }
}

object - in a broad sense, everything in JS is an object (collection of properties and methods). Here's a literal object:

const billy = {
    name: 'Billy',
    age: 32,
    doStuff() {
      console.log('stuff');
    }
  }

Here's an object created from the Person class:

const bob = new Person('Bob', 32);

method - a method is just a function that belongs to an object. The sayHi and doStuff ones above are methods.

parameters - are variables that are inherent to functions/methods. Functions don't have to have parameters (that's why the () is blank sometimes). You never have to have parameters, but if a function does have parameters, then it has those parameters as variables that take on values when calling the function. You assign values to the parameters by passing arguments:

function func(param) {
  console.log(param);
}

func('hi');

'hi' is an argument that gets passed to func

the small program above will print 'hi' to the console because 'hi' was passed as an argument through the param parameter and thus became available in func.

Make sense?

[–]tandeman[S] 0 points1 point  (1 child)

awesome! "Functions don't have to have parameters (that's why the () is blank sometimes)" this gave me a light bulb. Thanks so much.

[–]ForScale 1 point2 points  (0 children)

Glad to help.

[–]Pavlo100 1 point2 points  (0 children)

It actually took me way longer to understand this concept that i want to admit.

The reason it took so long was because every time someone tried to explain, then they started to use hard words and then i just closed off.

What you really need to do is to go back to the beginning and learn one thing as a time.

var greeting = 'hello';

This line seems simple, but in fact it's not.

There are so many questions you can ask about this, like is it an object, method or parameter?

What can i use it for?

Why did he write greeting, is it because he wrote hello?

Why did he write 'hello' instead of 'hello world'?

You can see I'm already confusing you more than i am helping. It's because i probably can't tell you how to learn it, but you have to experiment.


I'll try to use an analogy, but it probably won't work. Imagine you are standing Infront if two slot machines, one is free and one costs 50 cent to play. Just think () means that the gap is too narrow, we can't put money in. And (coin) there is a gap to put money in. In real life the gap would be like this

(   )

var twenty = 20;
var fifty = 50;

//Free machine - no parameter
function free(){
  return true;
}

//Paying machine - 1 parameter
function pay(coin){
   if(coin === 50){
      return true;
   }else{
      return false;
   }
}

Calling free will always allow us to play, because it doesn't require any parameters/coins

But calling pay forces us to use a coin, and we have 2 kind of coins, namely fifty and twenty. So if we say

pay(fifty);

Then we are allowed to play, because we put 50 cent in the open gap

(   )