日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

javascript 常量_JavaScript中的常量

發(fā)布時(shí)間:2023/12/1 javascript 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 javascript 常量_JavaScript中的常量 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

javascript 常量

JavaScript常數(shù) (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關(guān)鍵字聲明變量的唯一方法。 JavaScript的內(nèi)置變量提升功能可以使用var關(guān)鍵字執(zhí)行。 如果您不熟悉JavaScript中的變量,請(qǐng)瀏覽網(wǎng)站上的JavaScript中的變量文章。 如果您想知道什么是提升,請(qǐng)通讀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?

今天我們來(lái)看一下常量 。 ES15之后的變量可以兩種方式聲明: let和const 。 在深入研究const之前,讓我們了解一個(gè)常數(shù)是什么?

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.

大多數(shù)語(yǔ)言中的常量在其阻塞期間或生命持續(xù)存在的地方都可以保留其價(jià)值。 在JavaScript中,常量是我們以后無(wú)法直接修改的值。 這意味著,如果我們聲明一個(gè)具有特定值的常數(shù),則稍后在程序中分配其他一些值將導(dǎo)致錯(cuò)誤。

const a=10;a=25;

Output

輸出量

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

We get a TypeError.

我們得到一個(gè)TypeError 。

We declare a constant using the keyword const.

我們使用關(guān)鍵字const聲明一個(gè)常量。

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關(guān)鍵字聲明的常量必須同時(shí)初始化和聲明 。

const g=9.8;//is correct, butconst 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.

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

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

如果我們使用const關(guān)鍵字聲明一個(gè)對(duì)象,我們以后可以通過(guò)為其賦予新屬性來(lái)對(duì)其進(jìn)行更改。

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

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

向常量對(duì)象添加新屬性不會(huì)產(chǎn)生錯(cuò)誤。

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

同樣, 我們可以更改使用const關(guān)鍵字定義的數(shù)組的元素 。

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.

但是, 我們無(wú)法將新值重新分配給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.

會(huì)給出一個(gè)錯(cuò)誤。 但是,解決這個(gè)問(wèn)題的技巧是單獨(dú)更改數(shù)組的值,或者為該數(shù)組運(yùn)行一個(gè)循環(huán)。

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.

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

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

javascript 常量

總結(jié)

以上是生活随笔為你收集整理的javascript 常量_JavaScript中的常量的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。