sara356317 发表于 2024-2-17 14:10:39

快速提示:如何在 JavaScript 中声明变量

学习 JavaScript 时,基础知识之一是了解如何使用变量。变量是所有可能类型的值的容器,例如数字、字符串或数组(请参阅数据类型)。每个变量都有一个名称,稍后可以在应用程序中使用该名称(例如读取其值)。 Play Next Unmute Current Time 0:05 / Duration 2:00 Fullscreen Backward Skip 10s Play Video Forward Skip 10s 在本快速提示中,您将了解如何使用变量以及各种声明之间的差异。 声明、初始化和赋值之间的区别 在我们开始学习各种声明之前,让我们先看看变量的生命周期。 变量生命周期流程图 声明:变量在相应范围内使用给定名称进行注册(如下所述 - 例如在函数内部)。 初始化:当你声明一个变量时,它会自动初始化,这意味着 JavaScript 引擎为该变量分配内存。 赋值:这是为变量分配特定值的情况。 Learn to Code with JavaScript 声明类型 注意: whilevar自最初发布以来就在 JavaScript 中可用,let并且const仅在 ES6 (ES2015) 及更高版本中可用。请参阅此页面了解浏览器兼容性。


变量 句法这个声明可能是最流行的,因为在ECMAScript 6之前没有其他选择。用 声明的变量var在封闭函数的范围内可用。如果没有封闭函数,则它们在全局范围内可用。 例子这将导致错误ReferenceError: hello is not defined,因为该变量hello仅在函数内可用sayHello。但以 富人人数数据 下内容将起作用,因为变量将在全局范围内声明 - 位于同一范围内常量 句法: Learn to Code with JavaScript const x = "Hello World"; 从技术上讲,常量不是变量。常量的特殊性在于,声明时需要赋值,并且无法重新赋值。 Aconst仅限于封闭块的范围,例如let. 只要值在应用程序运行期间不得更改,就应该使用常量,因为在尝试覆盖它们时会收到错误通知。 意外的全球创造 您可以在全局上下文中(即在任何函数之外)编写所有上述命名声明,但即使在函数内,如果您忘记编写var,let或const在赋值之前,该变量将自动成为全局变量。 例子 上面的内容将输出Hello World到控制台,因为赋值之前没有声明hello ,因此该变量是全局可用的。

https://manchestermassage.net/wp-content/uploads/2024/02/%E5%8D%8E%E4%BE%A8%E8%B5%84%E6%96%99-1.png


注意:为了避免意外声明全局变量,您可以使用严格模式。 提升和颞死区 Learn to Code with JavaScript var和let/之间的另一个区别与变量提升const有关。变量声明将始终在内部提升(移动)到当前作用域的顶部。这意味着以下内容 此行为的一个迹象是两个示例都将记录undefined到控制台。如果var hello;不总是在顶部,它会抛出一个ReferenceError. 这种称为提升的行为适用于/var也适用于let/ const。如上所述,var在声明之前访问变量将返回undefined,因为这是 JavaScript 在初始化变量时分配的值。 但是在声明之前访问let/const变量将会引发错误。这是因为在代码中声明之前无法访问它们。进入变量的作用域和到达其声明之间的时间段称为临时死区,即变量不可访问的时间段。 您可以在揭秘 JavaScript 变量作用域和提升一文中阅读有关提升的更多信息。 结论 为了降低对错误的敏感性,您应该尽可能使用constand 。let如果您确实需要使用,var请务必将声明移至作用域的顶部,因为这可以避免与提升相关的不良行为。

FrankJScott 发表于 前天 03:32

Cool AI Stock Trading Platform Guide

For the lady asking about artificial intelligence and stock market, stock market trading ai, ai intelligence stocks, trading and investing in stock market, best way to analyze stocks, stock prediction tools, best ai tech stocks, best ai stock trading app, trading stock tips, ai to pick stocks,I highly suggest this updated AI stock trading analysis tips or artificial stocks, ai for stock market prediction, ai top stocks, ai based trading, stock market stock investment, free stock analysis sites, fundamentals of stock trading, stock ai software, ai stock prediction website, ai stocks nasdaq, not to mention this click for sourceon AI stock investing platform blog on top of best stock market analysis tools, ai investment stocks, stock market and how to invest, stock market data analysis, ai tool for stock market, free stock analysis sites, best ai companies to invest, ai companies to buy stock in, companies to invest in for ai, analyzing stocks for beginners, alongside all this excellent AI stock predictions analysis advice which is also great. Also, have a look at this next page on AI stock predictions platform advice together with stock programs, ai revolution stocks, stock for chat gpt, ai system for stock trading, best artificial intelligence trading software, ai investment stocks, best market analysis, best ai stock investments, best ai plays, analytics stocks, and don't forget this get more info on AI stock investing platform blog as well as ai stock trading software, artificial intelligence stocks to buy, ai bot for stock trading, ai in share market, stock trading help,my explanation for alongside all share analysis software, ai companies on nasdaq, best ai trader, investing in chat gpt, stock market ai bot,for good measure. Check more @ Awesome Laser Treatment Spots Face Sarasota Guide 242defe
页: [1]
查看完整版本: 快速提示:如何在 JavaScript 中声明变量