Do you really understand JavaScript

Tram Ho

Functions in JavaScript

image.png

If you are going to leave this article thinking you already know about functions in JavaScript then wait.

Are you sure you really understand function in JavaScript no?

Let’s find out with me.

1. Functions are just object ?

Screen Shot 2023-03-13 at 23.56.13.png

Function in JavaScript is simply object. Yes, you did not misread it. Function are really just objects in JavaScript and are a bit special compared to regular objects usually just different. We all know, object can have the right properties and methods no, so function too. In addition, Function also has other properties that are Name and Code. Property Name store the name of the Function and it can be left blank, meaning you can You can declare a function without having to give it a name (anonymous function) and property Code will save the code you write in your function. When you call Function, property code will be called and run the code you wrote in Function of As I said above, Function is just an object, so we can add properties as well as methods for a function, See the example below:

For example :

Output: Hello World

It’s quite interesting and also weird, isn’t it. In the example above, I just added add a property to the hello function. This is possible because in JavaScript, functions are just objects. You can also add a method to the same function like I did above.

2. Functions are First-Class Objects

First, what does First-Class Objects mean? First-Class Object simply means it is treated like an object and other common data types. That is, it can be passed in as an argument , return as a value , save to variables , save to data structures …. This means you can pass a function as an argument to another function (or in other words callback) , You can return a function inside a function, can save function into a variable, and so on and cloud…

3. Function Copied by Reference

As I said above, a function is just an object, so do you know what type of value object is? Exactly, object has type reference value. This means that Function is like so. That is if you assign Function A to variable B . The variable B will then point to the memory cell Function A and then if I change Function A, variable B will also be changed and vice versa (If you don’t understand, please google about Primitive and Reference Values Or I might do an article on the subject, which of course will be at a depth so that the you really understand about these two value types if you are interested)

For example :

Output: xinchao

Explanation: First I create Function hello , then I assign Function hello to variable hi , then I continue to add the xinchao property to hi .Finally I console.log(hello.xinchao) and I get xinchao . This is possible means that when I edit the variable hi, Function hello is also edited because like I said, Function has a value type of reference value

Conclude

Since I don’t want to write too long, I will only summarize the main ideas about functions in the article. Hope the knowledge I provide in this article will help you get the Overview and deeper understanding of Functions in JavaScript. Wish you all success on this.

Share the news now

Source : Viblo