• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

javaScript型参和实参

武飞扬头像
CQXXTXX
帮助1

 javascript函数的参数与大多数其他语言的函数的参数有所不同。函数不介意传递进来多少个参数,也不在乎传进来的参数是什么数据类型,甚至可以不传参数。

javascript中的函数定义并未指定函数形参的类型,函数调用也未对传入的实参值做任何类型检查。实际上,javascript函数调用甚至不检查传入形参的个数

  1.  
    function add(x){
  2.  
    return x 1;
  3.  
    }
  4.  
    console.log(add(1));//2
  5.  
    console.log(add('1'));//'11'
  6.  
    console.log(add());//NaN
  7.  
    console.log(add(1,2));//2

同名形参
  在非严格模式下,函数中可以出现同名形参,且只能访问最后出现的该名称的形参。

  1.  
    function add(x,x,x){
  2.  
    return x;
  3.  
    }
  4.  
    console.log(add(1,2,3));//3

  而在严格模式下,出现同名形参会抛出语法错误

  1.  
    function add(x,x,x){
  2.  
    'use strict';
  3.  
    return x;
  4.  
    }
  5.  
    console.log(add(1,2,3));//SyntaxError: Duplicate parameter name not allowed in this context

参数个数
  当实参比函数声明指定的形参个数要少,剩下的形参都将设置为undefined值

  1.  
    function add(x,y){
  2.  
    console.log(x,y);//1 undefined
  3.  
    }
  4.  
    add(1);

 常常使用逻辑或运算符给省略的参数设置一个合理的默认值

  1.  
    function add(x,y){
  2.  
    y = y || 2;
  3.  
    console.log(x,y);//1 2
  4.  
    }
  5.  
    add(1);

  [注意]实际上,使用y || 2是不严谨的,显式地设置假值(undefined、null、false、0、-0、”、NaN)也会得到相同的结果。所以应该根据实际场景进行合理设置。更多设置默认参数的方法:  ES5和ES6中函数对设置默认参数的方法总结
  当实参比形参个数要多时,剩下的实参没有办法直接获得,需要使用即将提到的arguments对象
  javascript中的参数在内部是用一个数组来表示的。函数接收到的始终都是这个数组,而不关心数组中包含哪些参数。在函数体内可以通过arguments对象来访问这个参数数组,从而获取传递给函数的每一个参数。arguments对象并不是Array的实例,它是一个类数组对象,可以使用方括号语法访问它的每一个元素

  1.  
    function add(x){
  2.  
    console.log(arguments[0],arguments[1],arguments[2])//1 2 3
  3.  
    return x 1;
  4.  
    }
  5.  
    console.log(add(1,2,3));//2

 arguments对象的length属性显示实参的个数,函数的length属性显示形参的个数

  1.  
    function add(x,y){
  2.  
    console.log(arguments.length)//3
  3.  
    return x 1;
  4.  
    }
  5.  
    add(1,2,3);
  6.  
    console.log(add.length);//2

形参只是提供便利,但不是必需的

  1.  
    function add(){
  2.  
    return arguments[0] arguments[1];
  3.  
    }
  4.  
    console.log(add(1,2));//3

对象参数
  当一个函数包含超过3个形参时,要记住调用函数中实参的正确顺序实在让人头疼

  1.  
    function arraycopy(/*array*/from,/*index*/form_start,
  2.  
    /*array*/to,/*index*/to_start,/*integer*/length){
  3.  
    //todo
  4.  
    }

通过名/值对的形式来传入参数,这样参数的顺序就无关紧要了。定义函数的时候,传入的实参都写入一个单独的对象之中,在调用的时候传入一个对象,对象中的名/值对是真正需要的实参数据

  1.  
    function easycopy(args){
  2.  
    arraycopy(args.from,args.form_start || 0,args.to,args.to_start || 0, args.length);
  3.  
    }
  4.  
    var a = [1,2,3,4],b =[];
  5.  
    easycopy({form:a,to:b,length:4});

同步
  当形参与实参的个数相同时,arguments对象的值和对应形参的值保持同步

  1.  
    function test(num1,num2){
  2.  
    console.log(num1,arguments[0]);//1 1
  3.  
    arguments[0] = 2;
  4.  
    console.log(num1,arguments[0]);//2 2
  5.  
    num1 = 10;
  6.  
    console.log(num1,arguments[0]);//10 10
  7.  
    }
  8.  
    test(1);

 [注意]虽然命名参数和对应arguments对象的值相同,但并不是说读取两个值会访问相同的内存空间。它们的内存空间是独立的,但值是同步的
  但在严格模式下,arguments对象的值和形参的值是独立的

  1.  
    function test(num1,num2){
  2.  
    'use strict';
  3.  
    console.log(num1,arguments[0]);//1 1
  4.  
    arguments[0] = 2;
  5.  
    console.log(num1,arguments[0]);//1 2
  6.  
    num1 = 10;
  7.  
    console.log(num1,arguments[0]);//10 2
  8.  
    }
  9.  
    test(1);

 当形参并没有对应的实参时,arguments对象的值与形参的值并不对应

  1.  
    function test(num1,num2){
  2.  
    console.log(num1,arguments[0]);//undefined,undefined
  3.  
    num1 = 10;
  4.  
    arguments[0] = 5;
  5.  
    console.log(num1,arguments[0]);//10,5
  6.  
    }
  7.  
    test();

内部属性


【callee】
  arguments对象有一个名为callee的属性,该属性是一个指针,指向拥有这个arguments对象的函数
  下面是经典的阶乘函数

  1.  
    function factorial(num){
  2.  
    if(num <=1){
  3.  
    return 1;
  4.  
    }else{
  5.  
    return num* factorial(num-1);
  6.  
    }
  7.  
    }
  8.  
    console.log(factorial(5));//120

 但是,上面这个函数的执行与函数名紧紧耦合在了一起,可以使用arguments.callee可以消除函数解耦

  1.  
    function factorial(num){
  2.  
    if(num <=1){
  3.  
    return 1;
  4.  
    }else{
  5.  
    return num* arguments.callee(num-1);
  6.  
    }
  7.  
    }
  8.  
    console.log(factorial(5));//120

但在严格模式下,访问这个属性会抛出TypeError错误

  1.  
    function factorial(num){
  2.  
    'use strict';
  3.  
    if(num <=1){
  4.  
    return 1;
  5.  
    }else{
  6.  
    return num* arguments.callee(num-1);
  7.  
    }
  8.  
    }
  9.  
    //TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
  10.  
    console.log(factorial(5));

 这时,可以使用具名的函数表达式

  1.  
    var factorial = function fn(num){
  2.  
    if(num <=1){
  3.  
    return 1;
  4.  
    }else{
  5.  
    return num*fn(num-1);
  6.  
    }
  7.  
    };
  8.  
    console.log(factorial(5));//120

【caller】


  实际上有两个caller属性
【1】函数的caller
  函数的caller属性保存着调用当前函数的函数的引用,如果是在全局作用域中调用当前函数,它的值是null

  1.  
    function outer(){
  2.  
    inner();
  3.  
    }
  4.  
    function inner(){
  5.  
    console.log(inner.caller);//outer(){inner();}
  6.  
    }
  7.  
    outer();
  8.  
    function inner(){
  9.  
    console.log(inner.caller);//null
  10.  
    }
  11.  
    inner();

在严格模式下,访问这个属性会抛出TypeError错误

  1.  
    function inner(){
  2.  
    'use strict';
  3.  
    //TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context
  4.  
    console.log(inner.caller);
  5.  
    }
  6.  
    inner();

【2】arguments对象的caller
  该属性始终是undefined,定义这个属性是为了分清arguments.caller和函数的caller属性

  1.  
    function inner(x){
  2.  
    console.log(arguments.caller);//undefined
  3.  
    }
  4.  
    inner(1);

 同样地,在严格模式下,访问这个属性会抛出TypeError错误

  1.  
    function inner(x){
  2.  
    'use strict';
  3.  
    //TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context
  4.  
    console.log(arguments.caller);
  5.  
    }
  6.  
    inner(1);

函数重载
  javascript函数不能像传统意义上那样实现重载。而在其他语言中,可以为一个函数编写两个定义,只要这两个定义的签名(接受的参数的类型和数量)不同即可
  javascript函数没有签名,因为其参数是由包含0或多个值的数组来表示的。而没有函数签名,真正的重载是不可能做到的

  1.  
    //后面的声明覆盖了前面的声明
  2.  
    function addSomeNumber(num){
  3.  
    return num 100;
  4.  
    }
  5.  
    function addSomeNumber(num){
  6.  
    return num 200;
  7.  
    }
  8.  
    var result = addSomeNumber(100);//300

只能通过检查传入函数中参数的类型和数量并作出不同的反应,来模仿方法的重载

  1.  
    function doAdd(){
  2.  
    if(arguments.length == 1){
  3.  
    alert(arguments[0] 10);
  4.  
    }else if(arguments.length == 2){
  5.  
    alert(arguments[0] arguments[1]);
  6.  
    }
  7.  
    }
  8.  
    doAdd(10);//20
  9.  
    doAdd(30,20);//50

参数传递
  javascript中所有函数的参数都是按值传递的。也就是说,把函数外部的值复制到函数内部的参数,就和把值从一个变量复制到另一个变量一样


【1】基本类型值


  在向参数传递基本类型的值时,被传递的值会被复制给一个局部变量(命名参数或arguments对象的一个元素)

  1.  
    function addTen(num){
  2.  
    num = 10;
  3.  
    return num;
  4.  
    }
  5.  
    var count = 20;
  6.  
    var result = addTen(count);
  7.  
    console.log(count);//20,没有变化
  8.  
    console.log(result);//30

【2】引用类型值


  在向参数传递引用类型的值时,会把这个值在内存中的地址复制给一个局部变量,因此这个局部变量的变化会反映在函数的外部

  1.  
    function setName(obj){
  2.  
    obj.name = 'test';
  3.  
    }
  4.  
    var person = new Object();
  5.  
    setName(person);
  6.  
    console.log(person.name);//'test'

当在函数内部重写引用类型的形参时,这个变量引用的就是一个局部对象了。而这个局部对象会在函数执行完毕后立即被销毁

  1.  
    function setName(obj){
  2.  
    obj.name = 'test';
  3.  
    console.log(person.name);//'test'
  4.  
    obj = new Object();
  5.  
    obj.name = 'white';
  6.  
    console.log(person.name);//'test'
  7.  
    }
  8.  
    var person = new Object();
  9.  
    console.log(person.name);//undefined
  10.  
    setName(person);
  11.  
    console.log(person.name);//'test'

侵删

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhghfjbc
系列文章
更多 icon
同类精品
更多 icon
继续加载