1.函数声明和函数表达式有什么区别
函数声明使用function来进行声明,函数声明提升,所以不论执行语句的位置皆可调用该函数。
function fn(){
console.log("hello world");
}
函数表达式通过赋值来声明函数,声明完毕后要加上分号表示该句结束,在表达式执行完后,函数才存在并可被调用
var fn=function(){
console.log("hello jirengu");
};
2.什么是变量的声明前置?什么是函数的声明前置
变量提升:当一个变量被定义时,在代码执行前会先将变量进行初始化再执行语句。
console.log(a);//undefined
var a = 1;
console.log(a);//1
//相当于
var a;
console.log(a);
a = 1;
console.log(a);
函数声明前置:JavaScript引擎将函数名视同变量名,所以采用function命令声明函数时,整个函数会像变量声明一样,被提升到代码头部。在用声明方式创建函数的前面或者后面都可以随时调用此函数;
fn();//hello world
function fu(){
console.log("hello world");
}
fn();//hello world
3.arguments 是什么
argument是类数组对象,每个函数中都存在argument对象,argument并不是一个真正的数组,所以不具备除length属性之外的属性,这个对象维护这所有传入该函数的参数列表。
通过以下语句可将arguments转化为数组对象
···
var args=Array.prototype.slice.call(arguments)
···
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.立即执行函数表达式是什么?有什么作用
立即执行函数表达式(Immediately-Invoked Function Expression),简称IIFE。表示定义函数之后,立即调用该函数。
作用:隔离作用域。
写法:
(function(){var a = 1;})()
(function fn1() {var a = 1;});
[function fn2() {var a = 1;}];
1, function fn3() {var a = 1;};
6.求n!,用递归来实现
function factor(n){
if(n === 1){
return 1;
}
return n*factor(n-1);
}
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 valley
getInfo('小谷', 3);//name: 小谷、age: 3、sex: undefined、["小谷", 3]、name valley
getInfo('男');//name: 男、age: undefined、sex: undefined、["男"]、name valley
8.写一个函数,返回参数的平方和?
function sumOfSquares(){
var sum = 0;
for(var i = 0;i < arguments.length;i++){
sum += arguments[i]*arguments[i];
}
return sum;
}
var result = sumOfSquares(2,3,4);
var result2 = sumOfSquares(1,3);
console.log(result) //29
console.log(result) //10
9.如下代码的输出?为什么
console.log(a);//undefined,变量a声明前置,d但未赋值
var a = 1;
console.log(b);//报错,Uncaught ReferenceError: b is not defined,原因:变量b,未声明,不能直接调用
10.如下代码的输出?为什么
sayName('world');//hello world,函数以声明的方式创建,函数会前置声明,调用时将参数“world”传入,输出结果: hello world。
sayAge(10);//报错,Uncaught TypeError: sayAge is not a function,原因:函数是以函数表达式的方式创建,不能提前调用。
function sayName(name){
console.log('hello ', name);
}
var sayAge = function(age){
console.log(age);
};
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
}
//声明foo时,得到下面
foo.[[scope]] = globalContext.AO;
//声明bar时,得到下面
bar.[[scope]] = globalContext.AO;
2.
barContext= {
AO: {
x:30
},
scope:bar.[[scope]] //globalContext.AO;
}
3.
fooContext = {
AO:{},
scope: foo.[[scope]]//globalContext.AO;
}
//在调用foo()时,console.log(x)中,变量x将先在fooContext.AO中搜索x,没有搜索到。再在globalContext.AO中搜索,globalContext.AO.x = 10。所以最后输出结果:10。
12.如下代码输出什么? 写出作用域链查找过程伪代码
var x = 10;
bar()
function bar(){
var x = 30;
function foo(){
console.log(x)
}
foo();
}
输出结果:30。
作用域链查找过程伪代码:
1.
globalContext = {
AO:{
x: 10,
bar: function
},
scope:null
}
//在声明bar时,得到下面
bar.[[scope]] = globalContext.AO;
2.
barContext = {
AO:{
x: 30,
foo: function
},
scope: globalContext.AO;
}
//在申明foo时,得到下面
foo.[[scope]] = barContext.AO
3.
fooContext = {
AO: {},
scope : barContext.AO
}
//在调用foo()时,console.log(x)中,变量x将先在fooContext.AO中搜索x,没有搜索到,再在barContext.AO中搜索。搜到到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的同时,得到
bar.[[scope]] = globalContex.AO
2.
barContext = {
AO: {
x: 30,
function(): function
},
scope: globalContex.AO
}
function().[[scope]] = barContext.AO;
3.
functionContext = {
AO: {},
scope: barContext.AO;
}
在匿名函数自调用时,变量x先搜索functionContext.AO,没有找到x,在搜索barContext.AO,x: = 30;所以输出结果为: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)
输出结果:undeifined、5、1、6、20、200
作用域链查找过程伪代码:
1.
globalContext = {
AO: {
a: 200,
fn: function,
fn3:function
}
scope: null
}
fn.[[scope]] = globalContext.AO
fn3.[[scope]] = globalContext.AO
2.
fnContext = {
AO: {
a: 20,
fn2: function
}
scope: globalContext.AO
}
fn2.[[scope]] = fnContext.AO
//第一次输出a为赋值,结果为:undefined。然后a赋值为5,第二次输入:为5。a++后a=6,var a后值不变,
3.
fn3Context= {
AO: { },
scope: globalContext.AO;
}
//第三次输处结果为1,然后将globalContext.AO.a赋值为200。
4.
fn2Context = {
AO: { },
scope: fnContext.AO
}
//第四次输处结果为6,然后将fnContext.AO.a赋值为20。
//第五次在fn中 console.log(a),变量a在fnContext.AO搜索,输出结果为20。
//第六次全局作用域下console.log(a),变量a在globalContext.AO中搜素,输出结果为200