函数与作用域

2017-07-20  本文已影响0人  cross_王

函数声明和函数表达式有什么区别

(function(){
alert('博主的名字是:myvin 。');
})()

这样可以使得全局变量不受局部变量的影响,保持全局的干净

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

arguments 是什么

函数的"重载"怎样实现

function overLoading() {
  // 根据arguments.length,对不同的值进行不同的操作
  switch(arguments.length) {
    case 0:
      /*操作1的代码写在这里*/
      break;
    case 1:
      /*操作2的代码写在这里*/
      break;
    }
}

立即执行函数表达式是什么?有什么作用

求n!,用递归来实现

function fn(n){
    if(n<=1){
      return 1;
}else{
   return n*fn(n-1);
     }
}

以下代码输出什么?

    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, '男');
getInfo('小谷', 3);
getInfo('男');

name,饥人谷
age,2
sex,男
["饥人谷", 2, "男", callee: function, Symbol(Symbol.iterator): function]
name,valley
name,小谷
age,3
sex,undefined
["小谷", 2, callee: function, Symbol(Symbol.iterator): function]
name,valley
name,男
age,undefined
sex,undefined
["男",callee: function, Symbol(Symbol.iterator): function]
name,valley


###写一个函数,返回参数的平方和?

function sumOfSquares(){
var result = 0;
for(var i = 0;i < arguments.length;i++){
result += Math.pow(arguments[i],2)
}
return result;
}
var result = sumOfSquares(2,3,4)
var result2 = sumOfSquares(1,3)
console.log(result) //29
console.log(result2) //10

###如下代码的输出?为什么

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

-  输出undefined并且报错。由于变量声明提前,所以第一句输出undefined。因为b没有声明,所以第三句报错

###如下代码的输出?为什么
sayName('world');
sayAge(10);
function sayName(name){
    console.log('hello ', name);
}
var sayAge = function(age){
    console.log(age);
};
-  输出hello  world并报错,因为函数声明提前,所以调用可以放在函数声明前面;函数表达式类似于变量声明,在函数表达式前调用等于undefined(),所以会报错

###如下代码输出什么? 写出作用域链查找过程伪代码

var x = 10
bar()
function foo() {
console.log(x)
}
function bar(){
var x = 30
foo()
}

-  输出10

function foo(){}
function bar(){}
var x
x =10
/*全局执行上下文
globalContext = {
AO:{
x:10,
foo():function,
bar():function
},
Scope:null
}
//声明 foo 时 得到下面
foo.[[scope]] = globalContext.AO
//声明 bar 时 得到下面
bar.[[scope]] = globalContext.AO
/
bar()
/
bar执行上下文
barContext = {
AO:{
x = 30
},
Scope: bar.[[scope]] //globalContext.AO
}
/
foo()
/
foo执行上下文
fooContext = {
AO:{},
foo.[[scope]] = globalContext.AO //找到a =10
}
*/

###如下代码输出什么? 写出作用域链查找过程伪代码

var x = 10;
bar()
function bar(){
var x = 30;
function foo(){
console.log(x)
}
foo();
}

-  输出30

function bar(){}
var x
x = 10
/*全局上下文
globalContext = {
AO:{
x : 10
bar() : function
},
Scope:null
//声明 bar 时 得到下面
bar.[[scope]] = globalContext.AO
}
/
bar()
/
bar上下文
barContext = {
AO:{
x : 30
foo():function
},
Scope: bar.[[scope]] //globalContext.AO
//声明 foo时 得到下面
foo.[[scope]] = barContext.AO
}
/
foo()
/
foo()上下文
fooContext = {
AO:{},
foo.foo.[[scope]] = barContext.AO //在barContext中找到下= 30
}
*/


###以下代码输出什么? 写出作用域链的查找过程伪代码

var x = 10;
bar()
function bar(){
var x = 30;
(function (){
console.log(x)
})()
}

-  输出30

function bar(){}
var x
x = 10
/*gloabContext = {
AO:{
x;10,
bar():function
},
Scope:null
//bar.[[scope]]=gloabContext.AO

/
bar()
/

barContext = {
AO:{
x:30,
匿名函数:function
},
bar.[[scope]] = globalContext.AO
// 匿名函数[[scope]]=barContext.AO
}
*/
匿名函数是立即执行函数,在barContext.AO中找到x =30


###以下代码输出什么? 写出作用域链查找过程伪代码

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

function fn(){}
function fn3(){}
var a
a = 3
/globalContext = {
AO:{
a:3
fn():function,
fn3():function
},
Scope:null
//fn[[scope]]=globalContext.AO
//fn3[[scope]]=globalContext.AO
}
/
fn(){
function fn2(){}
var a
console.log(a) //输出undefined
a = 5
console.log(a) //输出5
a++ //a = 6
fn3() //输出1 赋值globalContext.AO的a=200
fn2() //输出6 赋值fnContext.AO的a=20
console.log(a) //输出20
}
/fnContext = {
AO:{
fn2():function,
a:6
},
Scope:globalContext.AO
//fn2[[scope]]=fnContext.AO
}
/
/fn2Context = {
OA:{},
Scope:fnContext.AO //a未定义,在globalContext.AO中找到a=6
}
/
/fn3Context = {
OA:{},
Scope:globalContext.AO //a未定义,在globalContext.AO中找到a=1
}
/
console.log(a) //输出200

上一篇 下一篇

猜你喜欢

热点阅读