[Javascript căn bản]Function trong Javascript

Bài viết note kiến thức lập trình Javascript căn bản 1. Global scope vs function scope let myName ="longnguyen.dh";//global scopefunctionhello(){let myName2 ='longdhn.lk';//function scope console.log(myName);} console.log(myName2);//error Function scope chỉ truy xuất bên trong function, ra ngoài sẽ báo lỗi. Global scope truy xuất cả trong và ngoài function. 2. Closure Lexical scope sẽ định nghĩ scope

Bài viết note kiến thức lập trình Javascript căn bản

1. Global scope vs function scope

let myName ="longnguyen.dh";//global scopefunctionhello(){let myName2 ='longdhn.lk';//function scope
    console.log(myName);}

console.log(myName2);//error
  • Function scope chỉ truy xuất bên trong function, ra ngoài sẽ báo lỗi.
  • Global scope truy xuất cả trong và ngoài function.

2. Closure

  • Lexical scope sẽ định nghĩ scope của biến đó bởi vị trí của biến trong vị trí của chúng ta.
let newName ='Long Nguyen';//global scopefunctionsayHello(){let message ='Hi';//block scope
    console.log(`${message}${newName}`);}sayHello();
  • Closure là nhiều function được lồng vào nhau. Cho phép chúng ta truy xuất funciton bên trong ra function bên ngoài. Closure được tạo khi mỗi khi function được tạo.
functionsayHello2(){//parent functionlet message ='Hi';functionsayHi(){//child function
        console.log(message);}return sayHi;}let hello =sayHello2();hello();

Giải thích: function sayHi bên trong có quyền truy xuất biến message của function sayHello2 bên ngoài.

  • Function con có thể truy xuất scope của function cha.
functionsayHello3(message){return funciton hiYourName(name){
        console.log(`${message}${name}`);};}let hello =sayHello3('Welcome to javascript');hello('function');//Welcome to javascript function
functionanotherFunction(){let anotherMessage ='hello';functionsayHi(){//child function
        console.log(anotherMessage);}return sayHi;}let callFunc =anotherFunction();

3. Arrow function

  • Arrow function cũng là function là kiểu function ẩn danh – anonymous function – function không có tên. Ra đời từ 2015 ECMAScript.
constsquare=function(x){return x * x;}square(5);//25
  • Arrow function ngắn gọn hơn
constsquare=(x)=>{return x * x;}square(5);//25
  • Rút gọn khi return đơn giản
constsquare=(x)=> x * x;square(5);//25

Nguồn:

https://www.youtube.com/channel/UC8vjHOEYlnVTqAgE6CFDm_Qhttps://kt.city/course/tu-hoc-javascript-hieu-qua-va-de-dang-danh-cho-nguoi-moi

Nguồn: viblo.asia

Bài viết liên quan

9 Mẹo lập trình Web “ẩn mình” giúp tiết kiệm hàng giờ đồng hồ

Hầu hết các lập trình viên (kể cả những người giỏi) đều tốn thời gian x

Can GPT-4o Generate Images? All You Need to Know about GPT-4o-image

OpenAI‘s GPT-4o, introduced on March 25, 2025, has revolutionized the way we create visual con

Khi nào nên dùng main, section, article, header, footer, và aside trong HTML5

HTML5 đã giới thiệu các thẻ ngữ nghĩa giúp cấu trúc nội dung web một cách có

So sánh Webhook và API: Khi nào nên sử dụng?

Trong lĩnh vực công nghệ thông tin và phát triển phần mềm, Webhook và API là hai th