zoukankan      html  css  js  c++  java
  • Javascript中length属性的总结

    Javascript中length属性的总结

    一、StringObject中的length

        length属性是返回字符串的字符数目。

        例如:

    // 普通字符串
    var str = "abcdef";
    console.log(str.length); // 6
    // 数组
    var str1 =  new Array(1,2,3,4);
    console.log(str1.length); // 4
    // 数组与字符串
    var str2 = str1 + str; // "abcdef1,2,3,4"
    console.log(str2.length); // 13
    // 对象和对象
    var obj = {};
    console.log(obj.length); // undefined
    var obj += obj; // "[object Object][object Object]"
    console.log(obj.length); // 30

    二、Function中的length

         length可以返回function的参数数目。

    var a = function(a,b,c,d){};
    console.log(a.length); // 4
    var b = RegExp;
    console.log(b.length); //new RegExp(pattern, attributes)构造方法中有两个参数, 所以length为2

    ※ arguments实例的length属性则是返回调用程序传递给函数的实际参数数目。

    var a = function(){
       console.log(arguments.length); 
    };
    a(1,2,3); // 3
    a(); // 0

      注: 众所周知,在javascript中没有方法的重载,而arguments实例恰好可以帮我们来模拟方法的重载。

  • 相关阅读:
    JavaScript节点属性
    main函数的参数
    CGI
    open()函数 linux中open函数使用
    海伦公式
    C语言字符串操作函数
    makefile(一)
    makefile
    第一天
    时间小憩
  • 原文地址:https://www.cnblogs.com/MonkeyKingK/p/4928584.html
Copyright © 2011-2022 走看看