JavaScript中的全局變量介紹

Global variables are declared outside of a function for accessibility throughout the program, while local variables are stored within a function using var for use only within that function’s scope. If you declare a variable without using var, even if it’s inside a function, it will still be seen as global:

全局變量在函數外部聲明,以在整個程序中進行訪問,而局部變量使用var存儲在函數中,僅在該函數的范圍內使用 。 如果在不使用var情況下聲明變量,即使該變量位于函數內部,則仍將其視為全局變量:

var x = 5; // globalfunction someThing(y) {var z = x + y;console.log(z);
}function someThing(y) {x = 5; // still global!var z = x + y;console.log(z);
}function someThing(y) {var x = 5; // localvar z = x + y;console.log(z);
}

A global variable is also an object of the current scope, such as the browser window:

全局變量也是當前作用域的對象,例如瀏覽器窗口:

var dog = “Fluffy”;
console.log(dog); // Fluffy;var dog = “Fluffy”;
console.log(window.dog); // Fluffy

It’s a best practice to minimize global variables. Since the variable can be accessed anywhere in the program, they can cause strange behavior.

最佳做法是最小化全局變量。 由于可以在程序中的任何位置訪問變量,因此它們可能導致奇怪的行為。

References:

參考文獻:

  • var -Javascript|MDN

    var -Javascript | MDN

  • You Don’t Know JavaScript: Scopes & Closures

    您不懂JavaScript:范圍和閉包

javascript中的全局變量和window.variable有什么區別? (What’s the difference between a global var and a window.variable in javascript?)

The scope of JavaScript variables are either global or local. Global variables are declared OUTSIDE the function and its value is accessible/changeable throughout the program.

JavaScript變量的范圍是全局或局部。 全局變量在函數之外聲明,并且其值在整個程序中都可以訪問/更改。

You should ALWAYS use var to declare your variables (to make locally) else it will install GLOBALLY

您應該始終使用var聲明變量(在本地生成),否則它將全局安裝

Take care with the global variables because they are risky. Most of the time you should use closures to declare your variables. Example:

請注意全局變量,因為它們具有風險。 大多數時候,您應該使用閉包來聲明變量。 例:

(function(){var myVar = true;
})();

更多信息: (More Information:)

  • Visual guide to JavaScript variable definitions and scope

    可視化JavaScript變量定義和范圍指南

  • Intro to JavaScript variable definitions and hoisting

    JavaScript變量定義和提升簡介

翻譯自: https://www.freecodecamp.org/news/global-variables-in-javascript-explained/

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

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

相關文章

初識spring-boot

使用Spring或者SpringMVC的話依然有許多東西需要我們進行配置,這樣不僅徒增工作量而且在跨平臺部署時容易出問題。 使用Spring Boot可以讓我們快速創建一個基于Spring的項目,而讓這個Spring項目跑起來我們只需要很少的配置就可以了。Spring Boot主要有如…

leetcode 879. 盈利計劃(dp)

這是我參與更文挑戰的第9天 ,活動詳情查看更文挑戰 題目 集團里有 n 名員工,他們可以完成各種各樣的工作創造利潤。 第 i 種工作會產生 profit[i] 的利潤,它要求 group[i] 名成員共同參與。如果成員參與了其中一項工作,就不能…

區塊鏈101:區塊鏈的應用和用例是什么?

區塊鏈技術是一場記錄系統的革命。 比特幣是歷史上第一個永久的、分散的、全球性的、無信任的記錄分類帳。自其發明以來,世界各地各行各業的企業家都開始明白這一發展的意義。 區塊鏈技術的本質讓人聯想到瘋狂,因為這個想法現在可以應用到任何值得信賴的…

java請求接口示例_用示例解釋Java接口

java請求接口示例介面 (Interfaces) Interface in Java is a bit like the Class, but with a significant difference: an interface can only have method signatures, fields and default methods. Since Java 8, you can also create default methods. In the next block y…

如何建立搜索引擎_如何建立搜尋引擎

如何建立搜索引擎This article outlines one of the most important search algorithms used today and demonstrates how to implement it in Python in just a few lines of code.本文概述了當今使用的最重要的搜索算法之一,并演示了如何僅用幾行代碼就可以在Pyth…

用Docker自動構建紙殼CMS

紙殼CMS可以運行在Docker上,接下來看看如何自動構建紙殼CMS的Docker Image。我們希望的是在代碼提交到GitHub以后,容器鏡像服務可以自動構建Docker Image,構建好以后,就可以直接拿這個Docker Image來運行了。 Dockerfile 最重要的…

Linux學習筆記15—RPM包的安裝OR源碼包的安裝

RPM安裝命令1、 安裝一個rpm包rpm –ivh 包名“-i” : 安裝的意思“-v” : 可視化“-h” : 顯示安裝進度另外在安裝一個rpm包時常用的附帶參數有:--force : 強制安裝,即使覆蓋屬于其他包的文件也要安裝--nodeps : 當要安裝的rpm包依賴其他包時&#xff0…

leetcode 518. 零錢兌換 II

給定不同面額的硬幣和一個總金額。寫出函數來計算可以湊成總金額的硬幣組合數。假設每一種面額的硬幣有無限個。 示例 1: 輸入: amount 5, coins [1, 2, 5] 輸出: 4 解釋: 有四種方式可以湊成總金額: 55 5221 52111 511111 示例 2: 輸入: amount 3, coins [2] 輸出: 0 解…

軟件測試中什么是正交實驗法_軟件工程中的正交性

軟件測試中什么是正交實驗法正交性 (Orthogonality) In software engineering, a system is considered orthogonal if changing one of its components changes the state of that component only. 在軟件工程中,如果更改系統的組件之一僅更改該組件的狀態&#xf…

leetcode 279. 完全平方數(dp)

題目一 給定正整數 n,找到若干個完全平方數(比如 1, 4, 9, 16, …)使得它們的和等于 n。你需要讓組成和的完全平方數的個數最少。 給你一個整數 n ,返回和為 n 的完全平方數的 最少數量 。 完全平方數 是一個整數,其…

github代碼_GitHub啟動代碼空間

github代碼Codespaces works like a virtual Integrated Development Environment (IDE) on the cloud.代碼空間的工作方式類似于云上的虛擬集成開發環境(IDE)。 Until now, you had to make a pull request to contribute to a project. This required setting up the enviro…

php變量

什么叫變量&#xff1f; 變量可以通過變量名訪問。在指令式語言中&#xff0c;變量通常是可變的&#xff1b; 這里就先這么簡單理解&#xff0c;通過對語言的研究會更加的理解變量的其他意義。 在PHP中變量是用于存儲信息的"容器"&#xff1a; <?php $x5; $y6;…

js將base64做UrlEncode轉碼

使用 encodeURIComponent() 其詳細介紹 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent var base64 &#xff08;base64值&#xff09;encodeURIComponent(base64 ) //轉化 轉載于:https://www.cnblogs.com/xll-qg/p…

引用自己創建的css樣式表_如何使用CSS創建聯系表

引用自己創建的css樣式表First we create the HTML elements - input fields for First Name, Last Name, Email and a Text Area for the message.首先&#xff0c;我們創建HTML元素-名字&#xff0c;姓氏&#xff0c;電子郵件和消息的文本區域的輸入字段。 Later we apply C…

leetcode 1449. 數位成本和為目標值的最大數字(dp)

這是我參與更文挑戰的第12天 &#xff0c;活動詳情查看更文挑戰 題目 給你一個整數數組 cost 和一個整數 target 。請你返回滿足如下規則可以得到的 最大 整數&#xff1a; 給當前結果添加一個數位&#xff08;i 1&#xff09;的成本為 cost[i] &#xff08;cost 數組下標…

風能matlab仿真_風能產量預測—深度學習項目

風能matlab仿真DL DATATHON- AI4ImpactDL DATATHON- AI4影響 Published by Team AI Traders — Suyash Lohia, Nguyen Khoi Phan, Nikunj Taneja, Naman Agarwal and Mihir GuptaAI交易員團隊發布 -Suyash Lohia&#xff0c;Nguyen Khoi Phan&#xff0c;Nikonj Taneja&#x…

android JNI調用(Android Studio 3.0.1)(轉)

最近回頭復習了一下android 的jni調用&#xff0c;卻發現按以前的方法調用失敗&#xff0c;一怒之下就重新摸索&#xff0c;碰了幾次壁&#xff0c;發現網上好多教程都不能成功調用&#xff0c;于是記錄一下現在AS版本成功好用的調用方法。 這里設定你的ndk已經下載并且設置沒問…

安卓源碼 代號,標簽和內部版本號

SetupSecurityPortingTuningCompatibilityReference轉到源代碼Getting Started OverviewCodelines, Branches, and ReleasesCodenames, Tags, and Build NumbersProject RolesBrand GuidelinesLicensesFAQSite UpdatesDownloading and Building RequirementsEstablishing a Bui…

git 列出標簽_Git標簽介紹:如何在Git中列出,創建,刪除和顯示標簽

git 列出標簽Tagging lets developers mark important checkpoints in the course of their projects development. For instance, software release versions can be tagged. (Ex: v1.3.2) It essentially allows you to give a commit a special name(tag).通過標記&#xff…

leetcode 278. 第一個錯誤的版本(二分)

題目 你是產品經理&#xff0c;目前正在帶領一個團隊開發新的產品。不幸的是&#xff0c;你的產品的最新版本沒有通過質量檢測。由于每個版本都是基于之前的版本開發的&#xff0c;所以錯誤的版本之后的所有版本都是錯的。 假設你有 n 個版本 [1, 2, …, n]&#xff0c;你想找…