进阶任务3

作者: 机智的大口袋 | 来源:发表于2017-11-24 23:29 被阅读0次

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

相关文章

  • 进阶任务-3

    函数声明和函数表达式有什么区别 函数声明可以在在代码中任意位置调用,但是函数表达式只能在表达式语句后面调用 什么是...

  • 进阶任务3

    1. 函数声明和函数表达式有什么区别 函数声明:声明不必放到调用的前面; 函数表达式:声明必须放到调用的前面,因为...

  • 进阶任务3

    1.函数声明和函数表达式有什么区别函数表达式的声明必须放在调用前面,函数声明的声明不需要放在调用前面。2.什么是变...

  • 进阶任务3

    1、函数声明和函数表达式有什么区别2、什么是变量的声明前置?什么是函数的声明前置3、arguments 是什么4、...

  • 进阶任务3

    函数声明和函数表达式有什么区别## 函数声明:function functionName(){}函数表达式:var...

  • 进阶-任务3

    函数声明和函数表达式有什么区别 函数声明 函数声明命名了一个叫做print的函数,以后使用print()就可以调用...

  • 任务:进阶3:JS函数

    前言 在程序设计语言里面有一种语句叫决策语句,可以使程序根据是否满足特定的条件,运行相对应代码。换言之,决策语句使...

  • 进阶任务3(主线任务):函数与作用域

    任务 函数声明和函数表达式有什么区别答:函数声明:function functionName(){}  函数表达式...

  • Swift多线程:GCD进阶,单例、信号量、任务组

    Swift多线程:GCD进阶,单例、信号量、任务组 Swift多线程:GCD进阶,单例、信号量、任务组

  • 进阶-任务2

    1、JavaScript 定义了几种数据类型? 哪些是原始类型?哪些是复杂类型?原始类型和复杂类型的区别是什么? ...

网友评论

    本文标题:进阶任务3

    本文链接:https://www.haomeiwen.com/subject/adpevxtx.html