一、函数声明和函数表达式有什么区别
声明函数的方法有三种:函数声明,构造函数,函数表达式。
函数声明方法是function+函数名+(){ }
函数表达式是将一个函数赋值给一个变量,如var printName=function(){ }
函数声明的时候如不调用是不会执行的,调用函数位置不论放在函数声明前或后,均可被调用,而函数表达式要先赋值来声明函数,表达式执行完后,函数才存在并可被调用;
在执行时,函数声明会将函数声明前置,而函数表达式不会函数声明前置。
二、什么是变量的声明前置?什么是函数的声明前置
在同一个作用域下,var声明的变量和function声明的函数都会前置,不管位置在前在后,执行的时候,变量的声明会先前置,接着是函数声明前置,先把所有的var放前面,再把所有的函数声明function放前面,再执行后面的代码,如果变量名和函数名一致,第2个出现的会覆盖第1个出现的。
变量声明前置

函数的声明前置

变量前置和函数声明前置都使上述情形未报错。
三、arguments 是什么
arguments类似于一个数组存储了函数调用时所传递的参数,在函数内部可以使用arguments对象获取到该函数的所有传入参数。当参数数量不确定时可以用arguments,且arguments是默认的,在执行的时候会自动传递进去的参数。
四、函数的"重载"怎样实现
重载是指具有相同名字的函数,由于传入的参数不同,需要不止一次执行函数。
JavaScript中函数不存在重载,如果一个只有3个参数的函数,但是输出项给的是4个内容,那么JS中后面的会把前面的覆盖。函数通过名字确定唯一性,同名函数会被覆盖。
五、立即执行函数表达式是什么?有什么作用
(function( ){ })( );
立即执行函数是定义函数后立即执行。作用是隔离作用域,避免全局污染。
定义后什么时候执行这一点类似于函数表达式。
六、求n!,用递归来实现

七、以下代码输出什么?
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('男');

八、写一个函数,返回参数的平方和?
function sumOfSquares(){
}
var result = sumOfSquares(2,3,4)
var result2 = sumOfSquares(1,3)
console.log(result) //29
console.log(result) //10

九、如下代码的输出?为什么
console.log(a);
var a = 1;
console.log(b);

十、 如下代码的输出?为什么
sayName('world');
sayAge(10);
function sayName(name){
console.log('hello ', name);
}
var sayAge = function(age){
console.log(age);
};

十一、 如下代码输出什么? 写出作用域链查找过程伪代码
var x = 10
bar()
function foo() {
console.log(x)
}
function bar(){
var x = 30
foo()
}

十二、 如下代码输出什么? 写出作用域链查找过程伪代码
var x = 10;
bar()
function bar(){
var x = 30;
function foo(){
console.log(x)
}
foo();
}

十三、 以下代码输出什么? 写出作用域链的查找过程伪代码
var x = 10;
bar()
function bar(){
var x = 30;
(function (){
console.log(x)
})()
}

十四、以下代码输出什么? 写出作用域链查找过程伪代码
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)

网友评论