Differences between methods and commands in JavaScript


I'm starting to study JavaScript, but in several books and web queries the terms methods and commands appear, but from what I see both use () and perform the same action.

For example, this is a command:

var name = "Ejemplo"; alert("Hello " + name); 

and it's a method:

var name ="Ejemplo"; name.toUpperCase(); 

Can you tell me what is the difference between them, please?

asked by Polo Moreno 31.12.2015 в 01:59

1 answer


Well I do not know how accurate my answer will be, but this is my bit to clarify a bit your confusion and try to address yourself better.

Just like you say, both use parentheses (). In this case what goes inside the parentheses represent the arguments, or in other words the input data. These input data are processed and yield a result. Imagine a black box where you throw things, that transforms what you get and throws something new.

What is the process behind the scenes? Well for now you can just ignore it. At this moment we are not very interested in how that is achieved, but simply the final result.

For example, with alert("Hola mundo") what happens is that you enter the text: "Hello world" and the result will be that it shows you a small alert window with the same message that you entered.

On the other hand, if you have a variable name = "polo" and you execute name.toUpperCase() , what happens is the following:

  • Since there is nothing inside the parentheses, it means that you did not enter input data.
  • Then the question is: What will I transform if I do not enter anything? In this case, if you look after name there is a point. This point is connecting toUpperCase() to name and it is intuited that there is a relationship between the two.
  • What is that relationship? good is simple, toUpperCase() translates to "convert to uppercase", it could be assumed that this operation will capitalize what is before the point. And so, if you run name.toUpperCase() you'll get polo becomes POLO .
  • Already broken down a bit, I will mention two new terms: function and method.

    A function is a block of code designed to execute a particular task, for example alert . What you were calling him command . A function receives some arguments, transforms them and throws a result.

    On the other hand, a method is a function within an object. This means that the input data is extracted from the same object from which it comes. For example with name.toUpperCase() the object is name . name is a text string that has a toUpperCase method, which transforms it by changing it to upper case. That is, it does not need arguments because the input data was taken from the same object.

    However, there are times when you need more information to be able to get the function to do what you want it to do, and this information can not simply infer from the object.

    For example, the repeat(count) method needs to be defined the number of times that the chain with which you are working is repeated:

    'abc'.repeat(0);    // ''
    'abc'.repeat(1);    // 'abc'
    'abc'.repeat(2);    // 'abcabc'
    answered by 31.12.2015 / 03:17