美文网首页
进阶3-函数声明,作用域

进阶3-函数声明,作用域

作者: 24_Magic | 来源:发表于2017-03-08 00:01 被阅读22次

1.函数声明和函数表达式有什么区别
使用function关键字可以声明一个函数(声明不必放到调用的前面)

<p>//函数调用
sayHello()
//函数声明
function sayHello(){
  console.log("hello")
}</p>

函数表达式(声明必须放到调用的前面),不然就是输出undefined

var sayHello = function(){
  console.log("hello");
}
sayHello()

2.什么是变量的声明前置?什么是函数的声明前置

Paste_Image.png

3.arguments 是什么
类数组对象,可以使用arguments来获得函数的所有传入参数

function ptintInfo(name,age,sex){
  console.log(name);
  console.log(age);
  console.log(sex);
  console.log(arguments[0])
  console.log(arguments[1])
  console.log(arguments[2])
}
printInfo('Eric',28,'male')

4.函数的"重载"怎样实现

function printPeopleInfo(name,age,sex){
  if(name){
    console.log(name);
  }
  if(age){
    console.log(age);
  }
  if(sex){
    console.log(sex);
  }
}
printPeopleInfo('Byron',26);
printPeopleInfo('Byron',26,'male');

通过不同的参数调用执行相应的逻辑
5.立即执行函数表达式是什么?有什么作用

(function fn1() {})();
(function fn2() {});//括号
[function fn3() {}];//数组
1,function fn4() {};//逗号

隔离作用域
6.求n!,用递归来实现

function factor(n) {
  if (n === 1){
    return 1 ;
  }
  return  factor (n-1)*n
}

for循环

function factor(n) {
  var result = 1;
  for (var i = 0,i<n,i++){
    result=result*i
  }
}

7.

function getInfo(name, age, sex){
        console.log('name:',name);
        console.log('age:', age);
        console.log('sex:', sex);
        console.log(arguments);
        arguments[0] = 'valley';
        console.log('name', name);
    }
getInfo('饥人谷',2,'男')
//name:饥人谷 age:2 sex:男 ['饥人谷',2,'男'] name vally
getInfo('小谷',3)
//name:小谷 age:3 sex:undefined  ['小谷',3] name vally
getInfo('男')
//name:undefined age:undefined sex:男 ['男'] name vally

8

function sumOfSquares(a,b,c){
      if(a){
        a=a*a;
      }
      else{
        a=0;
      }
      if(b){
        b=b*b;
      }
       else{
        b=0;
      }
      if(c){
        c=c*c;
      }
       else{
        c=0;
      }
    console.log(a+b+c);
    }
var result = sumOfSquares(3)
  console.log(result)//9
var result = sumOfSquares(2,3,4)
  console.log(result) //29
var result= sumOfSquares(1,3)
   console.log(result) //10

9

console.log(a)
var a=1
console.log(b)

等价于

var a
console.log(a)
//undefined;var a声明前置,但未被赋值
a=1
console.log(b)
//报错;前面b没有被赋值

10

sayName('world');
    sayAge(10);
    function sayName(name){
        console.log('hello ', name);
    }
    var sayAge = function(age){
        console.log(age);
    };
//hello world ,报错;sayName是一个function声明的函数,不需要前置;sayAge是var声明的一个函数,需要前置

11

var x = 10
bar() 
function foo() {
  console.log(x)
}
function bar(){
  var x = 30
  foo()
}
//10;
//1.globalContext={
  AO:{
    x=10
    foo:function
    bar:function},
Scope: null
}
2.barContext={
    AO:{
      x=30;
    },
  scope:bar.[[scope]]=globalContext.AO
3.fooContext={
    AO:{},
    Scope:foo.[[scope]]=globalContext.AO
}//调用foo的时候找到globalContext.AO里面x=10,输出10

12

var x = 10;
bar() 
function bar(){
  var x = 30;
  function foo(){
    console.log(x) 
  }
  foo();
}
//1.globalContext={
  AO:{
    x=10
    bar:function},
Scope: null
}
bar.[[scope]]=globalScope.AO
2.barContext={
    AO:{
      x=30;
      foo:function
    },
  scope:bar.[[scope]]=globalContext.AO
3.fooContext={
    AO:{},
    Scope:foo.[[scope]]=barContext.AO
}//foo()找到barContext.AO中x=30,输出30

13

var x = 10;
bar() 
function bar(){
  var x = 30;
  (function (){
    console.log(x)
  })()
}
//30;
//1.globalContext={
  AO:{
    x=10
    bar:function},
Scope: null
}
bar.[[scope]]=globalScope.AO
2.barContext={
    AO:{
      x=30;
    },
  scope:bar.[[scope]]=globalContext.AO
//查找到x=30

14

var a = 1;
function fn(){
  console.log(a)
  var a = 5
  console.log(a)
  a++
  var a
  fn3()
  fn2()
  console.log(a)
  function fn2(){
    console.log(a)
    a = 20
  }
}
function fn3(){
  console.log(a)
  a = 200
}
fn()
console.log(a)
//undefined,5,1,6,20,200
//1.globalContext={
  AO:{
   a=1
   fn:function
   fn3:function},
Scope: null
}
fn.[[scope]]=globalContext.AO
fn3.[[scope]]=globalContext.AO
2.fnContext={
    AO:{
      a//输出undefined------Ⅰ
      a=5//输出5------Ⅱ
      //fn3()在globalContext.AO中找到a=1,*(这时候globalContext中a=200)*输出1-----Ⅲ
      //fn2(),a++后a=6,*(这时候fnContext.AO中a=20)*输出6-----Ⅳ
      //console.log(a);输出20-----Ⅴ
      fn2:function
    },
  scope:fn.[[scope]]=globalContext.AO
3.fn3Context={
    AO:{}
  Scope.fn3[[scope]]=globalContext.AO
4.fn2Context={
    AO:{}
  Scope.fn2[[scope]]=fnContext.AO
//console.log(a)输出globalContext.AO中a=200-----Ⅵ

相关文章

  • 进阶3-函数声明,作用域

    1.函数声明和函数表达式有什么区别使用function关键字可以声明一个函数(声明不必放到调用的前面) 函数表达式...

  • es6块级作用域定义声明函数

    允许在块级作用域内声明函数。函数声明类似于var,即会提升到全局作用域或函数作用域的头部。同时,函数声明还会提升到...

  • 箭头函数

    箭头函数本身没有this, 由箭头函数声明时所处作用域决定。作用域(两个): ① 函数作用域 ② 全局作用域箭头函...

  • 第九天,函数作用域和声明提前

    函数作用域和函数声明提前。 函数作用域,分为全局作用域和部分作用域,在系统执行函数时会自动创建一个作用域,在执行完...

  • 变量声明、声明提前和作用域

    一. 作用域 分为全局作用域和函数作用域 函数作用域简言之就是:变量在声明他们的函数体以及这个函数体嵌套的任意函数...

  • javascript的三种作用域

    javascript 三种作用域 全局作用域 函数作用域 块级作用域(es6) 全局作用域 变量声明不写在函数内部...

  • 进阶3-函数与作用域

    1.函数声明和函数表达式有什么区别 函数声明:function sayHello(){ console.log('...

  • Scope

    1. 全局作用域 全局变量拥有全局作用域 2. 函数作用域 在函数内声明的变量用于函数作用域。在函数体内,局部变量...

  • JavaScript函数作用域和声明提前

    函数作用域 JavaScript 中没有块级作用域,JavaScript 取而代之地使用了函数作用域。变量在声明它...

  • zj3 函数与作用域

    讲解函数声明、函数表达式、声明前置、作用域、作用域链相关概念 函数声明和函数表达式有什么区别 什么是变量的声明前置...

网友评论

      本文标题:进阶3-函数声明,作用域

      本文链接:https://www.haomeiwen.com/subject/idkugttx.html