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

2017-03-08  本文已影响22人  24_Magic

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-----Ⅵ
上一篇 下一篇

猜你喜欢

热点阅读