Variable hoisting Global variables Constants

Another unusual thing about variables in JavaScript is that you can refer to a variable declared later, without getting an exception. This concept is known as hoisting; variables in JavaScript are in a sense "hoisted" or lifted to the top of the function or statement. However, variables that aren‘t initialized yet will return a value of undefined.

/**
 * Example 1
 */
console.log(x === undefined); // logs "true"
var x = 3;

/**
 * Example 2
 */
// will return a value of undefined
var myvar = "my value";

(function() {
  console.log(myvar); // undefined
  var myvar = "local value";
})();

  

The above examples will be interpreted the same as:

/**
 * Example 1
 */
var x;
console.log(x === undefined); // logs "true"
x = 3;

/**
 * Example 2
 */
var myvar = "my value";

(function() {
  var myvar;
  console.log(myvar); // undefined
  myvar = "local value";
})();

  

Because of hoisting, all var statements in a function should be placed as near to the top of the function as possible. This best practice increases the clarity of the code.

Global variables

Global variables are in fact properties of the global object. In web pages the global object iswindow, so you can set and access global variables using the window.variable syntax.

Consequently, you can access global variables declared in one window or frame from another window or frame by specifying the window or frame name. For example, if a variable called phoneNumber is declared in a document, you can refer to this variable from an iframe as parent.phoneNumber.

Constants

You can create a read-only, named constant with the const keyword. The syntax of a constant identifier is the same as for a variable identifier: it must start with a letter, underscore or dollar sign and can contain alphabetic, numeric, or underscore characters.

const prefix = ‘212‘;

  

A constant cannot change value through assignment or be re-declared while the script is running. It has to be initialized to a value.

The scope rules for constants are the same as those for let block scope variables. If theconst keyword is omitted, the identifier is assumed to represent a variable.

You cannot declare a constant with the same name as a function or variable in the same scope. For example:

// THIS WILL CAUSE AN ERROR
function f() {};
const f = 5;

// THIS WILL CAUSE AN ERROR ALSO
function f() {
  const g = 5;
  var g;

  //statements
}

  

时间: 2024-10-17 04:41:50

Variable hoisting Global variables Constants的相关文章

Variable hoisting Function hoisting

Variable hoisting Another unusual thing about variables in JavaScript is that you can refer to a variable declared later, without getting an exception. This concept is known as hoisting; variables in JavaScript are in a sense "hoisted" or lifted

Accessing Lua global variables from c++

http://blog.csdn.net/cnjet/article/details/5909541 Calling Lua scripts from c++’s example was written in post How to embed Lua 5.1 in C++. Now, let us look at how to access Lua’s global variables from c++. Value passing between c++ and Lua rely on Lu

Effective JavaScript Item 12 理解Variable Hoisting

本系列作为Effective JavaScript的读书笔记. JavaScript中并没有Block Scoping,只有Function Scoping. 因此如果在一个Block中定义了一个变量,那么这个变量相当于是被定义到了这个Block属于的Function中,比如: function isWinner(player, others) { var highest = 0; for (var i = 0, n = others.length; i < n; i++) { <span s

How to declare global variables in Android? --- Application Subclasses

I wrote this answer back in '09 when Android was relatively new, and there were many not well established areas in Android development. I have added a long addendum at the bottom of this post, addressing some criticism, and detailing a philosophical

mysql &nbsp; show global variables

以下变量全部来自mysql5.5.40-log 包括该版本的所有变量, 绝大部分变量都是解释的,那些没有解释的变量不是很重要, 不过后期还是会补充上去的.以下变量只有部分经过本人验证, ,没有验证均来自大师的博客. 仅供参考. 1.自增值相关 auto_increment_increment 1   #   增量 auto_increment_offset 1      #  起始值/偏移量 http://chengxuyuan.naxieshir.com/fenlei/2/p/151.html

《ruby编程语言》笔记 1

赋值: ruby支持并行赋值,即允许在赋值表达式中出现多余一个值和多于一个的变量: x,y=1,2a,b=b,ax,y,z=[1,2,3] (python同样可以正常上面的语句). Methods in Ruby are allowed to return more than one value, and parallel assignmentis helpful in conjunction with such methods. For example:# Define a method to

python main函数中变量默认为global variable

在python的main函数中的变量默认为全局变量,而其他的def函数中的变量则默认为局部变量. 当然,局部变量会优先于全局变量,在执行formal_print(t_global)语句时便可看出. 测试代码如下: <span style="font-size:18px;">#coding=utf-8 #测试python的全局变量,局部变量的机制 def formal_print(s_global):#常规的传参用法,传递参数进行print,变量名可任意 print &quo

PHP 笔记一(systax/variables/echo/print/Data Type)

PHP stands for "Hypertext Preprocessor" ,it is a server scripting language. What Can PHP Do? PHP can generate dynamic page content PHP can create, open, read, write, delete, and close files on the server PHP can collect form data PHP can send an

[Ruby] Ruby Variable Scope

Scope defines where in a program a variable is accessible. Ruby has four types of variable scope, local,global, instance and class. In addition, Ruby has one constant type. Each variable type is declared by using a special character at the start of t