Skip to content

[js] 第63天 举例子说明javascript的变量声明提升和函数声明提升 #281

Open
@haizhilin2013

Description

@haizhilin2013
Collaborator

第63天 举例子说明javascript的变量声明提升和函数声明提升

Activity

xn213

xn213 commented on Jun 17, 2019

@xn213
var getName = function(){
  console.log(4)
}

function getName() {
  console.log(5)
}

getName() // 4 函数声明优先级高于var声明,  故 4 覆盖了 5
AnsonZnl

AnsonZnl commented on Jun 18, 2019

@AnsonZnl
Contributor

变量声明

console.log(a);// undefinde
var a= "hello world";
console.log(a);//  "hello world"

函数声明

f()
function f(){
    console.log('hello world');// "hello world"
}

变量声明只提升声明 不提升赋值操作,函数声明 函数体整体被提升。

getName();
var  getName = function(){
    console.log("小明");
}
function getName(){
    console.log("大明");
}
getName();

提升之后变成:

var getName;
function getName(){
    console.log("大明");
}
getName();//”大明“
getName= function(){
    console.log("小明");
}
getName();//"小明"
wenyejie

wenyejie commented on Jun 18, 2019

@wenyejie
var getName = function(){
  console.log(4)
}

function getName() {
  console.log(5)
}

getName() // 4 函数声明优先级高于var声明,  故 4 覆盖了 5

不是4的优先级是高于5, 而是5的优先级高于4,
5先声明, 但是后来它被4覆盖而已

forever-z-133

forever-z-133 commented on Jun 18, 2019

@forever-z-133

先声明函数名,再声明 var 变量名,然后按顺序从上到下赋值。

var a = 1;
function a() { }
console.log(a);  // 1
yxkhaha

yxkhaha commented on Jun 18, 2019

@yxkhaha
console.log(a);//undefined 变量a提升
var a = 10;

fn();//666 函数能在声明之前调用说明函数声明提升了
function fn() {
   console.log('666')
}
Vi-jay

Vi-jay commented on Jul 31, 2019

@Vi-jay
console.log(a);//a会被提升到作用域最前面声明 var a = undefined
var a = 1;
console.log(a);
var a = 1;

// 变量和函数都会被提升 函数优先
function a() {
  
}
smile-2008

smile-2008 commented on Dec 31, 2020

@smile-2008
var getName = function(){
  console.log(4)
}

function getName() {
  console.log(5)
}

getName() // 4 函数声明优先级高于var声明,  故 4 覆盖了 5
xiaoqiangz

xiaoqiangz commented on Jun 16, 2022

@xiaoqiangz

// 函数表达式 -- 匿名函数
var sum = function() {
console.log('sum2')
}
// 函数声明 --函数名 变量提升
function sum() {
console.log('sum')
}
sum()

var name = 'xxx'
function variable() {
  if (name == undefined) {
    var name = 'qqq'
    console.log(`hello ${name}`)
  } else {
    console.log(`hello ${name}`)
  }
}
variable()
Frontendnightmare

Frontendnightmare commented on Jun 14, 2023

@Frontendnightmare

@AnsonZnl

变量声明

console.log(a);// undefinde
var a= "hello world";
console.log(a);//  "hello world"

函数声明

f()
function f(){
    console.log('hello world');// "hello world"
}

变量声明只提升声明 不提升赋值操作,函数声明 函数体整体被提升。

getName();
var  getName = function(){
    console.log("小明");
}
function getName(){
    console.log("大明");
}
getName();

提升之后变成:

var getName;
function getName(){
    console.log("大明");
}
getName();//”大明“
getName= function(){
    console.log("小明");
}
getName();//"小明"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    jsJavaScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @smile-2008@wenyejie@haizhilin2013@xiaoqiangz@forever-z-133

        Issue actions

          [js] 第63天 举例子说明javascript的变量声明提升和函数声明提升 · Issue #281 · haizlin/fe-interview