Classes - JavaScript | MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

Hoisting

An important difference between function declarations and class declarations is that function declarations are hoisted and class declarations are not. You first need to declare your class and then access it, otherwise code like the following will throw aReferenceError:

const p = new Rectangle(); // ReferenceError

class Rectangle {}

https://developer.mozilla.org/en-US/docs/Glossary/Hoisting

Hoisting

Hoisting is a term you will not find used in any normative specification prose prior to ECMAScript® 2015 Language Specification. Hoisting was thought up as a general way of thinking about how execution contexts (specifically the creation and execution phases) work in JavaScript. However, the concept can be a little confusing at first.

Conceptually, for example, a strict definition of hoisting suggests that variable and function declarations are physically moved to the top of your code, but this is not in fact what happens. Instead, the variable and function declarations are put into memory during the compile phase, but stay exactly where you typed them in your code.

Learn moreSection

Technical exampleSection

One of the advantages of JavaScript putting function declarations into memory before it executes any code segment is that it allows you to use a function before you declare it in your code. For example:

function catName(name) {
console.log("My cat's name is " + name);
} catName("Tigger"); /*
The result of the code above is: "My cat's name is Tigger"
*/

The above code snippet is how you would expect to write the code for it to work. Now, let's see what happens when we call the function before we write it:

catName("Chloe");

function catName(name) {
console.log("My cat's name is " + name);
}
/*
The result of the code above is: "My cat's name is Chloe"
*/

Even though we call the function in our code first, before the function is written, the code still works. This is because of how context execution works in JavaScript.

Hoisting works well with other data types and variables. The variables can be initialized and used before they are declared.

Only declarations are hoistedSection

JavaScript only hoists declarations, not initializations. If a variable is declared and initialized after using it, the value will be undefined. For example:

console.log(num); // Returns undefined
var num;
num = 6;

If you declare the variable after it is used, but initialize it beforehand, it will return the value:

num = 6;
console.log(num); // returns 6
var num;

The below two examples demonstrate the same behavior.

var x = 1; // Initialize x
console.log(x + " " + y); // '1 undefined'
var y = 2; // Initialize y // The above example is implicitly understood as this:
var x; // Declare x
var y; // Declare y
// End of the hoisting. x = 1; // Initialize x
console.log(x + " " + y); // '1 undefined'
y = 2; // Initialize y

Technical referenceSection

Document Tags and Contributors

 Last updated by: Tymofek, Aug 17, 2018, 2:22:46 PM
 
 
 


最新文章

  1. JqGrid自定义的列
  2. 张艾迪(创始人): 整合全新的UIW.AD概念
  3. iOS_UIImage_jpg<-->png转换
  4. win7下如何建立ftp服务器
  5. 【CodeVS】p1174 靶形数独
  6. HTML5学习记录1-新特性
  7. ReactNative之从“拉皮条”来看RN中的Spring动画
  8. C# 以管理员权限删除文件
  9. TypeScript -访问修饰符
  10. Ubuntu11.04安装引导BURG
  11. Mysql 强行Kill 连接
  12. lame定理求欧几里得算法的求余和赋值次数
  13. 删除数据库字段一样的row, 并增加唯一索引
  14. Oracle sqlloader
  15. Day14 集合(一)
  16. 猫咪记单词——NABCD模型分析
  17. fundamentals5
  18. -174dBm的含义
  19. BZOJ 3813--奇数国(线段树&欧拉函数&乘法逆元&状态压缩)
  20. 定期删除Azure存储账号下N天之前的数据文件-ASM

热门文章

  1. MapReduce编程模型及其在Hadoop上的实现
  2. ELK最佳实践
  3. Topcoder SRM 145 DIV 1
  4. POJ 1155 TELE [树状DP]
  5. Yii2 数据操作Query Builder
  6. Linux命令之basename 命令
  7. 【Python】继承
  8. JSConsole调试
  9. typedef 与 define 的区别
  10. 一种调用dll的巧妙方法