函数声明和函数表达式有什么区别
用函数声明创建的函数可以在定义之前就进行调用(声明前置);而用函数表达式创建的函数不能在被赋值之前进行调用(会返回错误,因为前置只定义了变量undefined,不能在undefined上进行函数调用)。
什么是变量的声明前置?什么是函数的声明前置
在一个作用域下,var 声明的变量和function 声明的函数会前置
比如var a=3,会前置声明变量a=undefined,直到运行到 var a=3才会赋值3给a(函数表达式前置声明的是var变量undefined,并不是函数,所以不能调用函数)
function a (){} ,会前置声明a: function。
arguments 是什么
arguments对象是所有函数中可用的局部变量。使用arguments对象在函数中引用函数的参数。此对象包含传递给函数的每个参数的条目,第一个条目的索引从0开始
函数的"重载"怎样实现
可以根据arguments对象的length值进行判断
function peopleInfo (name,age,sex){
if(arguments.length === 1){
console.log (name);
}
if (arguments.length === 2){
console.log (name+age);
}
if(arguments.length === 3){
console.log (name+age+sex);
}
}
立即执行函数表达式是什么?有什么作用
声明一个函数,在后面加上一个小括号,表示立即调用这个函数,作用是隔离作用域
为了兼容JS的语法,还需要另外加些符号(不加的话会报错),写法为
(function a (){}());
!function a (){}();
[function a (){}()];
求n!,用递归来实现
function factor(n){
if(n === 1) {
return 1
}
return n * factor(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,男
name valley
name:小谷
age:3
小谷,3
name valley
name: 男
name valley
*/
function sumOfSquares(numb1,numb2,numb3){
if (arguments.length === 3){
console.log(numb1*numb1+numb2*numb2+numb3*numb3)
}
if(arguments.length ===2){
console.log(numb1*numb1+numb2*numb2)
}
}
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 变量a未赋值 , 报错 没有变量b
sayName('world');
sayAge(10);
function sayName(name){
console.log('hello ', name);
}
var sayAge = function(age){
console.log(age);
}; //hello,world , 报错 sayAge是一个变量无法调用函数
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.[[scope]] = globalContext.AO
bar.[[scope]] = globalContext.AO
2. barContent={
AO: {
x:30
}
bar.[[scope]] = globalContext.AO
}
3. fooContent={
AO:{}
foo.[[scope]] = globalContext.AO
}
*/
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.[[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]] // globalContext.AO
}
*/
var x = 10;
bar()
function bar(){
var x = 30;
(function (){
console.log(x)
})()
}// 30
1. globalContent ={
AO:{
x:10
bar:function
}
}
bar[[scope]]=globalContent.AO
2. barContent ={
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
/*
globalContent
a:200
fn:function
fn3:function
---------------
fnContent
a:20
fn2:function
---------------
fn3Content
---------------
fn2Content
*/