JavaScript Methods

Properties in JavaScript are sets of pairs of named values inside an object.

For example,

var person {firstName: “first-name”, lastName: “last-name”, age: 36, id: 45};

Every pair of named value in the object above is a property of the object. As an example, we could mention,

firstName: “first-name”

Every property has a value. For example in this case, the value of the property is “first-name”.

A JavaScript method is a property containing function definition. For example we could add a new property to the object above as follows:

var person {firstName: “first-name”, lastName: “last-name”, age: 36, id: 45, fullName: function() {return this.firstName + ” ” + this.lastName;}};

In other words, methods are functions stored as object properties.

Now, in order to access the object method created above, you need to write a line of code something like the following:

person.fullName();    //Pay attention to the set of parentheses added to the end.

So you could say that fullName is a property of the object person and fullName() -same name having a set of parentheses- is a method of that object. This also means that the property fullName will function as a function when you add the pair of parentheses to the end.

 

An Explanation of the Concept of Hoisting in JavaScript

JavaScript gives you a lot of freedom in programming which in some cases can also be detrimental your own code.

Other programming languages like C, C++ or Java are much more strict. There, you don’t have much freedom but in turn your end results are almost always predictable.

One of those cases in JavaScript is Hoisting. It’s kind of weird that you are allowed to declare a variable anywhere in a block of code and use it anywhere in the code block even before you declared it. That’s because JavaScript “hoists” variables. In other words, it moves all instances of variable declarations to the beginning of the code block before execution. It’s interesting to note that all instances of declarations are hoisted but no instances of initialization is hoisted even when declaration and initialization take place in one line of code. That means if you declare and initialize a variable,

var x = 5;

in the end of your script, anywhere in the script, your variable exists and is declared but JavaScript knows the value 5 only after the line of code that initialized the variable x.

Well, that’s something that you should understand and take note of, to avoid bugs in your scripts. So to avoid any bugs, declare and initialize all of the variables that you plan to use inside the script in the beginning of the block.

Methods in JavaScript

toString() is an example of a method in JavaScript. It’s a Boolean method and what it does is convert Booleans to Strings. Take a look at the following example:

false.toString();  // Returns “false”.

Please pay attention to have the method has been used here and that’s how methods are used in JavaScript overall. First you have the keyword false then you have a dot and then the method has been added to it. The dot actually shows that toString() is a method.