js 预编译环节的变量提升

有些东西需要先告诉你怎么用,再去理解定义

关于变量的提升
function test () {
  console.log(a)
  var a = 100
  console.log(a)
};
test () // undefined  // 100
执行步骤
function test () {
  var a = undefined
  console.log(a)
  a = 100
  console.log(a)
}

关于函数的提升
function test () {
  console.log(a)
  function a () {}
  console.log(a)
  function b () {}
  console.log(b)
};
test() // function a () {} // function a () {} // function b () {}
执行步骤
function test () {
  var a = undefined
  var b = undefined
  a = function a () {}
  b = function b () {}
  console.log(a)
  console.log(a)
  console.log(b)
}

关于变量和函数的混合提升
function test () {
  console.log(a)
  var a = 100
  console.log(a)
  function a () {}
  console.log(a)
  function b () {}
  console.log(b)
};
test() // function a () {}  // 100  // 100 // function b () {}
执行步骤
function test () {
  var a = undefined
  var b = undefined
  a = function a () {}
  b = function b () {}
  console.log(a)
  a = 100
  console.log(a)
  console.log(a)
  console.log(b)
}

关于形参和变量混合提升
function test (a) { // 定义函数中用来接收参数 这个就是形参
  console.log(a)
  var a = 100
  console.log(a)
};
// 调用函数的时候传具体的参数 这个参数就是实参
test(1) // 1 // 100
执行步骤
function test (a) {
  var a = undefined
  a = a // 右边的a是形参
  console.log(a)
  a = 100
  console.log(a)
}

关于形参和函数的混合提升
function test (a) {
  console.log(a)
  function b () {}
  function a () {}
  console.log(a)
  console.log(b)
};
test(1) // functioin a () {} // function a () {} // function b () {}
执行步骤
function test (a) {
  var a = undefined
  var b = undefined
  a = a // 右边的a是形参
  b = function b () {}
  a = function a () {}
  console.log(a)
  console.log(a)
  console.log(b)
}

关于形参和变量和函数的混合提升
function test (a) {
  console.log(a);
  var a = 100;
  console.log(a)
  function a () {}
  console.log(a)
  function b () {}
  console.log(b)
};
test(1) // function a () {} // 100 // 100 // function b () {}
执行步骤
function test (a) {
  var a = undefined
  var b = undefined
  a = a // 右边的a是形参
  a = function a () {}
  b = function b () {}
  console.log(a)
  a = 100
  console.log(a)
  console.log(a)
  console.log(b)
}

if 判断语句不影响变量提升 (if是执行语句,而变量提升属于预编译环节;js先编译后执行)
function test () {
  console.log(a)
  if (false) {
    var a = 100
    console.log(a)
  }
};
test() // undefined
执行步骤
function test () {
  var a = undefined
  console.log(a)
  if (false) {
    a = 100
    console.log(a)
  }
}

总结:
函数内部js的预编译环节执行顺序如下
1. 将形参,变量提升到该函数的顶部(执行期上下文顶部)
2. 将形参,函数赋值到对应的变量 (先形参,后函数)
  

原文地址:https://www.cnblogs.com/guozongzhang/p/11022932.html

时间: 2024-08-14 12:02:36

js 预编译环节的变量提升的相关文章

js预编译环节 变量声明提升 函数声明整体提升

预编译四部曲 1.创建AO对象 2.找形参和变量声明,将变量和形参名作为AO属性名,值为undefined 3.将实参和形参统一 4.在函数体里面找函数声明,值赋予函数体 function fn(a){ console.log(a) //function a(){} var a = 123; console.log(a)//123 function a(){} console.log(a)//123 var b = function(){} console.log(b) //function()

js预编译

先来做三个测试 eg1: 1 var a; 2 a = 1; 3 4 function a() {}; 5 console.log(a); eg2: 1 var a; 2 function a() {}; 3 console.log(a); eg3: 1 var a; 2 function a() {}; 3 a = 1; 4 console.log(a); eg1:输出1   eg2:输出a方法   eg3:输出1 为什么输出结果是这样的? 先来看看js预编译实现过程: 1.js首先扫描var

typeof、摇树优化、js预编译

typeof:  typeof用以获取一个变量或者表达式的类型,一元运算符 null:空.无.表示不存在,当为对象的属性赋值为null,表示删除该属性 undefined:未定义.当声明变量却没有赋值时会显示该值.可以为变量赋值为undefined number:数值.最原始的数据类型,表达式计算的载体 string:字符串.最抽象的数据类型,信息传播的载体 boolean:布尔值.最机械的数据类型,逻辑运算的载体 object:对象.面向对象的基础 注意:没有数组,因为type数组时结果是ob

Handlebars.js 预编译(转)

Handlebars.js 官网上对预编译1是这样说的: 你需要安装 Node.js 你需要在全局环境中,通过 Npm 安装 handlebars 包 然后你就可以通过命令预编译你的 handlebars 模板文件: $ handlebars <input> --output <output> 假设我有一个模板文件,名称为 person.handlebars,内容很简单,如下: <table> <tr> <td>This is {{firstna

Js 预编译

开始 所有的测试,都是chrome浏览器. js本身是脚本语言,可以不经过编译直接运行,但是在进入每一个<script>标签,或者js文件的时候,这样的情况下,浏览器会给js一个预编译的过程. 这样就有必要讨论一下预编译了. 解析的状态是首先引入一个代码段,也就是之前提到的标签或者文件.读入整个代码段,进行解析,解析的方式是遇到变量就声明, 对变量声明为undefined,对函数做标记,这个就是预解析,但是如果声明了同名的函数,使用最后一个.变量都是undefied,只有触发的时候,才会激活.

JS预编译 ---2018.6.22

1.a标签有特点是  在podding和margin情况下不能撑开 2.今天学习了JS的预编译感觉贼迷糊,先听课后面多做练习解决问题: 创建ao对象; 找形参和变量声明,将变量名和形参名作为AO属性名,值为undefined; 将参考值和形参统一; 在函数体里面找函数声明,赋值于函数体. 原文地址:https://www.cnblogs.com/zs-xanadu/p/9215739.html

js预编译和函数执行

javascript 执行过程 1.语法检测(有没有基本的语法错误,例如中文,关键字错误...)2.词法分析(预编译) (1)创建全局GO(global object)对象 (2)对var声明的变量进行声明提升但不赋值,放入GO对象中 (3)对函数体整体进行提升,放入GO对象中 3.逐行执行 一.全局 直接是script标签中的代码,不包括函数执行 <script type="text/javascript"> console.log(a); var a = 100; co

js 杂症,this with 变量提升

一.this.xx 和 xx 是两回事 受后端语言影响,总把this.xx 和xx 当中一回事,认为在function中,xx 就是this.xx,其实完全两回事: this.xx 是沿着this 原型链找变量,xx是沿着作用域链找变量 var func = function(){ console.info(this); } var func1 = function(){ function func11(){ console.info(this) // 应该是func1 this.func();

关于js预编译以及js文件执行顺序的几个问题。

关于js的执行原理,除去html页面中直接添加的代码,js代码的放置可以分为两类. //情形a           <script type="text/javascript" src="xxx.js"$amp;>amp;$lt;/script>           //情形b            <script type="text/javascript">           code......