js函数和作用域

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

用函数声明创建的函数可以在定义之前就进行调用(声明前置);而用函数表达式创建的函数不能在被赋值之前进行调用(会返回错误,因为前置只定义了变量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

*/
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、函数声明和函数表达式 函数声明的形式:function fn() { } ,使用函数声明的形式声明一个函数时,...
    sutingy阅读 1,405评论 0 0
  • 1.函数声明和函数表达式有什么区别 函数声明和函数表达式是声明函数的两种不同的方式,形式如下: 函数声明:即使用f...
    饥人谷_bigJiao阅读 1,643评论 0 0
  • 一、函数声明function(){}是function +函数名字(){内容}调用函数是 函数名字();funct...
    崔敏嫣阅读 2,405评论 0 0
  • 函数声明和函数表达式 函数声明: fuction fn(){console.log("test");} 函数表达式...
    赵BW阅读 2,425评论 0 0
  • 熟悉的人更得心应手,要学会适应这种状况
    不知飞阅读 1,193评论 0 0

友情链接更多精彩内容