javascript 常量_JavaScript中的常量

javascript 常量

JavaScript常數 (JavaScript Constants)

Before ES15, the only way to declare variables using the var keyword. JavaScript's inbuilt feature of hoisting variables could be carried out using the var keyword. If you're unfamiliar with variables in JavaScript, have a glance at Variables in JavaScript article on the website. If you wish to know what hoisting is, read through Hoisting in JavaScript.

在ES15之前,這是使用var關鍵字聲明變量的唯一方法。 JavaScript的內置變量提升功能可以使用var關鍵字執行。 如果您不熟悉JavaScript中的變量,請瀏覽網站上的JavaScript中的變量文章。 如果您想知道什么是提升,請通讀JavaScript中的提升 。

Today we'll look at constants. Variables after ES15 could be declared in two ways - let and const. Before we dive deeper into const, let's understand what a constant is?

今天我們來看一下常量 。 ES15之后的變量可以兩種方式聲明: letconst 。 在深入研究const之前,讓我們了解一個常數是什么?

Constants in most languages are something that retains their value during their block or wherever their life persists. In JavaScript, constants are values that we cannot modify later directly. This means if we declare a constant with a certain value, assigning some other value later in the program would result in an error.

大多數語言中的常量在其阻塞期間或生命持續存在的地方都可以保留其價值。 在JavaScript中,常量是我們以后無法直接修改的值。 這意味著,如果我們聲明一個具有特定值的常數,則稍后在程序中分配其他一些值將導致錯誤。

    const a=10;
a=25;

Output

輸出量

    Uncaught TypeError: Assignment to constant variable. at <anonymous>:1:2

We get a TypeError.

我們得到一個TypeError

We declare a constant using the keyword const.

我們使用關鍵字const聲明一個常量。

const follows block scope. Consider the following,

const遵循塊作用域。 考慮以下,

var t=10;	//Value of t here is 10
{
const t=12;	//Value of t here is 12
}
//Value of t here is 10

Also, constants declared using the const keyword must be initialized and declared at the same time.

另外, 使用const關鍵字聲明的常量必須同時初始化和聲明

    const g=9.8;
//is correct, but
const g;
g=9.8i
//is incorrect.

However, an important catch to note that the const keyword is not what it looks like. The more accurate statement regarding const would be that it defines a constant reference to a value. This means that primitive values assigned to a variable using the const keyword cannot be altered but with objects, we are bound to no such restriction.

但是,需要注意的重要一點是const關鍵字不是它的外觀。 關于const的更準確的陳述是,它定義了對值常量引用 。 這意味著使用const關鍵字分配給變量的原始值不能更改,但是對于對象,我們不受任何限制。

If we declare an object with the const keyword, we can later change it by giving it new properties.

如果我們使用const關鍵字聲明一個對象,我們以后可以通過為其賦予新屬性來對其進行更改。

    const person={
Name: 'Fuzzy Sid',
Age: 20
}
person.gender='Male';

Adding a new property to a constant object does not yield us an error.

向常量對象添加新屬性不會產生錯誤。

Similarly, we can change the elements of an array defined using the const keyword.

同樣, 我們可以更改使用const關鍵字定義的數組的元素

    const arr=[1,2,3,4];
console.log(arr);       //[1,2,3,4]
arr[0]=0;
console.log(arr);       //[0,2,3,4]
arr.push_back(5);
console.log(arr);       //[0,2,3,4,5]

However, we cannot reassign new values to the array.

但是, 我們無法將新值重新分配給array

    arr = [9,8,7,6]

Output

輸出量

Uncaught TypeError: Assignment to constant variable.at <anonymous>:1:4

Would give an error. But the trick around this would be to individually change the values of the array or maybe run a loop for the same.

會給出一個錯誤。 但是,解決這個問題的技巧是單獨更改數組的值,或者為該數組運行一個循環。

    for(let i=0,cnt=9; i<arr.length; i++,cnt--){
arr[i]=cnt;
}

Remember that const variables are not supported in versions of Internet Explorer before 10 and they are not hoisted. So you cannot use them before the declaration.

請記住,Internet Explorer 10之前的版本不支持const變量,也不會使用它們。 因此,您不能在聲明之前使用它們。

翻譯自: https://www.includehelp.com/code-snippets/constants-in-javascript.aspx

javascript 常量

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377473.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377473.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377473.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

GDB與遠程(交叉)GDB調試

GDB提供的功能 1、啟動的程序&#xff0c;可以按照自定義的要求運行程序 2、可以讓被調試的程序在指定的斷點處停住(斷點可以是條件表達式) 3、當程序被停住時&#xff0c;可以檢查這個時候程序中發生的事 4、動態地改變程序的運行環境。 遠程&#xff08;交叉&#xff09;GD…

OTR-Linux控制臺打印顏色區分.

What I write, what I lost. 對于依靠打印來作debug的主要手段的, 能夠區分打印中的debug信息和error信息便顯得非常重要. 原文的介紹有一篇關于控制臺顏色的文章http://www.ibm.com/developerworks/cn/linux/l-tip-prompt/tip01/ 有定義實現各種顏色的方式. 以此為基礎, 方式挺…

c#異常處理_C#中的異常處理

c#異常處理What an exception is? 有什么例外&#xff1f; An exception is a runtime error; that means an abnormal situation which is created at run time and the program doesn’t execute successfully. Due to the exceptions, our program gets crash. 異常是運行…

(轉)走進AngularJs(六) 服務

原文地址&#xff1a;http://www.cnblogs.com/lvdabao/p/3464015.html 今天學習了一下ng的service機制&#xff0c;作為ng的基本知識之一&#xff0c;有必要做一個了解&#xff0c;在此做個筆記記錄一下。 一、認識服務&#xff08;service&#xff09; 服務這個概念其實并不陌…

Linux驅動程序框架以及概述

目錄驅動程序三種基本類型&#xff08;組成&#xff09;設備驅動程序功能驅動程序的內核模塊機制&#xff08;開發模式&#xff09;驅動程序框架三個主要部分1、字符設備驅動程序框架2、塊設備驅動程序框架2、網絡設備驅動程序框架驅動程序三種基本類型&#xff08;組成&#x…

curl 使用整理(轉載)

我一向以為&#xff0c;curl只是一個編程用的函數庫。 最近才發現&#xff0c;這個命令本身&#xff0c;就是一個無比有用的網站開發工具&#xff0c;請看我整理的它的用法。 curl網站開發指南 阮一峰 整理 curl是一種命令行工具&#xff0c;作用是發出網絡請求&#xff0c;然…

Linux內核邏輯結構

linux內核從邏輯上可以分為5個部分&#xff1a; 1、進程調度 進程調度控制進程對CPU的訪問。當需要選擇下一個進程運行時&#xff0c;由調度程序選擇最值得運行的程序。可運行進程實際上是僅等待CPU資源的進程&#xff0c;如果某個進程在等待其他資源&#xff0c;則該進程是不可…

對批量文件重命名

一、 文件夾下存放各種不同名稱的同類型文件 F:\test 二、重命名格式從a0開始&#xff0c;數字依次遞增&#xff0c;a0,a1,a2,a3… import ospathr"F:\test"#要修改文件的路徑 namer"a"#命名從什么開始 num0#默認從0開始&#xff0c;即a0,a1,a2...... …

替換Quartus 自帶編輯器 (轉COM張)

正文 此處以Quartus II 11.1和Notepad v5.9.6.2為例。 1. 使用QII自動調用Notepad來打開HDL、sdc、txt等文件&#xff1b;并且可以在報錯的時候&#xff0c;Notepad可以直接高亮所報錯的行&#xff08;此模式下&#xff0c;Notepad最大化后效果最佳&#xff09;。 方法&#xf…

scala 方法重載_Scala中的方法重載

scala 方法重載Scala方法重載 (Scala method overloading) Method overloading is a method that is redefined in different ways under the same name. Method overloading is one of the methods used to implement polymorphism in Scala. 方法重載是一種使用相同名稱以不…

C#網頁自動登錄和提交POST信息的多種方法 新人學習中

網頁自動登錄和提交POST信息的核心就是分析網頁的源代碼&#xff08;HTML&#xff09;&#xff0c;在C#中&#xff0c;可以用來提取網頁HTML的組件比較多&#xff0c;常用的用WebBrowser、WebClient、HttpWebRequest這三個。 以下就分別用這三種方法來實現&#xff1a;1、WebBr…

四、采集和制作數據集

一、采集數據 安裝labelme&#xff1a;pip install labelme 打開labelme&#xff1a;labelme 將收集好的照片(320320&#xff0c;png格式)存放到一個文件夾中&#xff0c;例如我的是F:\test&#xff0c;再此文件夾下再創建個文件夾label用于存放標簽文件 使用labelme打開數據…

MTFBWU的完整形式是什么?

MTFBWU&#xff1a;愿力量與您同在 (MTFBWU: May The Force Be With You) MTFBWU is an abbreviation of “May The Force Be With You". MTFBWU是“愿力量與你同在”的縮寫 。 It is an expression, which is commonly used in messaging or chatting on social media n…

VMware14.0 安裝 CentOS7.2

大致流程 對于VMware14.0安裝包用百度網盤下載即可。 鏈接&#xff1a;https://pan.baidu.com/s/1DEGa47EbI1Fup_MTXhv0xg 提取碼&#xff1a;izo6 華為云CentOS7 下載劃線的。其他步驟與大致流程里一樣。 最后輸入root 以及配置的密碼即可&#xff1a;密碼輸入時是沒有任何顯…

基于visual Studio2013解決C語言競賽題之1049抓牌排序

&#xfeff;&#xfeff;&#xfeff;&#xfeff;&#xfeff;&#xfeff;題目解決代碼及點評/* 功能&#xff1a;插入排序。許多玩牌的人是以這樣的方式來對他們手中的牌進行排序的&#xff1a;設手中原有3張牌已排好序&#xff0c;抓1張新牌&#xff0c;若這張新牌的次序在…

學習Lucene筆記一:創建索引

public class HelloLucene {/*** 建立索引* param args*/public void index(){IndexWriter writer null; try {//1.創建Directory,// Directory directory new RAMDirectory();//索引是建立在內存中的Directory directory FSDirectory.open(new File("D:/Lucene/ind…

【C++進階】C++創建文件/屏幕輸出流類(將信息同時輸出到文件和屏幕)

在軟件的調試技術中&#xff0c;很重要的一個技術是將軟件運行過程中的一些信息寫入到“日志文件”中。但是同時還要將信息顯示到屏幕上&#xff0c;以方便程序員實時查看這些信息。 最簡單的一種辦法是這樣的&#xff1a; std::ofstream output("debug.log", ios::…

五、加載數據集

之前寫過加載數據集的一些小筆記&#xff0c;這里詳細內容就不再敘述了 詳細學習可以參考該博文二、PyTorch加載數據 一、分析 因為U-net網絡架構是輸入1通道&#xff0c;大小為(572,572)的灰度圖&#xff0c;圖片大小無所謂&#xff0c;我的思路是將三通道的圖像使用OpenCV進…

CDMA的完整形式是什么?

CDMA&#xff1a;碼分多址 (CDMA: Code Division Multiple Access) CDMA is an abbreviation of Code Division Multiple Access. Code Division Multiple Access is a digital cellular technology and displays a network of multiple accesses. The various radio communica…

BCD碼與十進制的相互轉換

BCD碼是用每四位代替一位十進制數&#xff08;0 到 9 的某一位數&#xff09; 例如&#xff1a;0x25 就代表25 十六進制的每位轉換成二進制代表四個位。 下面是bcd轉char short int long c語言程序 //************************************************************…