1.函数声明和函数表达式有什么区别
函数表达式的声明必须放在调用前面,函数声明的声明不需要放在调用前面。
2.什么是变量的声明前置?什么是函数的声明前置
在一个作用域下,var 声明的变量和function 声明的函数会前置
console.log(a)
var a=1 //由于var声明提前,输出结果为undefined
sayHello();
function sayHello(){console.log('hello')} //输出为hello
3.arguments 是什么
arguments是函数的一个内置参数的数组对象,通过arguments[1、2、3]等...我们可以获取到相应的传入参数.
4.函数的"重载"怎样实现
在 JS 中没有重载! 同名函数会覆盖。 但可以在函数体针对不同的参数调用执行相应的逻辑
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(){var a=1;}]()
1,function(){var a=1;}
作用是隔离作用域
6.求n!,用递归来实现
function(n){
if(n===1){return 1}
return n*function(n-1)
}
function getInfo(name, age, sex){
console.log('name:',name); //输出name
console.log('age:', age); //输出age
console.log('sex:', sex); //输出sex
console.log(arguments); //输出整个arguments
arguments[0] = 'valley'; //arguments第一项变为valley
console.log('name', name); 输出arguments[0],即valley
}
getInfo('饥人谷', 2, '男'); //输出name:饥人谷 age:2 sex:男 ["饥人谷," 2 ",男"] name:valley
getInfo('小谷', 3); //输出name:小谷 age:3 sex:undefined ["小谷", 3] name:valley
getInfo('男'); //输出name:undefined age:undefined sex:男 ["男",] name:valley
function sumOfSquares(){
var result = 0;
for(var i = 0;i<arguments.length;i++{
result += arguments[i]*arguments[i]; }
return result;
}
console.log(a); //输出undefined,var a前置,然后执行console.log(a); 后执行a=1
var a = 1;
console.log(b); //error b未被赋值
sayName('world');
sayAge(10);
function sayName(name){
console.log('hello ', name);} //输出 hello world 声明前置,然后调用输出hello world
var sayAge = function(age){
console.log(age); }; //error 函数表达式的调用在声明前无法执行
var x = 10
bar()
function foo() {
console.log(x)
}
function bar(){
var x = 30
foo()
}
//
1.globalContext={
AO:{x:10
foo:function
bar:function
}
foo.[[scope]]=globalContext.AO
bar.[[scope]]=globalContext.AO
}
2.barContext={
AO:{x:30
}
Scope:bar.[[scope]]=globalContext.AO
}
3.fooContext={
AO:{}
Scope:foo.[[scope]]=globalContext.AO
}
//
输出为10
var x = 10;
bar()
function bar(){
var x = 30;
function foo(){
console.log(x)
}
foo();
}
//
1.globalContext={
AO:{x:10
bar:function
}
bar.[[scope]]=globalContext.AO
}
2.barContext={
AO:{x:30
foo:function
}
Scope:bar.[[scope]]=globalContext.AO
foo.[[scope]]=barContext.AO
}
3.fooContext={
AO:{}
Scope:foo.[[scope]]=barContext.AO
}
//
输出为30
var x = 10;
bar()
function bar(){
var x = 30;
(function (){
console.log(x)
})()
}
//
1.globalContext={
AO:{x:10
bar:function
}
bar.[[scope]]=globalContext.AO
}
2.barContext={
AO:{x:30
function(){}
}
Scope:bar.[[scope]]=globalContext.AO
function.[[scope]]=barContext.AO
}
3.functionContext={
AO:{}
Scope:function.[[scope]]=barContext.AO
}
//
输出为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)
//
1.globalContext={
AO:{a:1
fn:function
fn3:function
}
fn.[[scope]]=globalContext.AO
fn3.[[scope]]=globalContext.AO
}
2.fnContext={
AO:{a:5 -> 6
fn2:function
}
Scope:fn.[[scope]]=globalContext.AO
fn2.[[scope]]=fnContext.AO
}
3.fn2Context={
AO:{}
Scope:fn2.[[scope]]=fnContext.AO
}
4.fn3Context={
AO:{}
Scope:fn3.[[scope]]=globalContext.AO
}
//
输出undefined 5 1 6 20 200
网友评论