当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JavaScript 基础(二) - 创建 function 对象的方法, String对象, Array对象

JavaScript 基础(二) - 创建 function 对象的方法, String对象, Array对象

2019年01月22日  | 移动技术网IT编程  | 我要评论

创建 function 对象的两种方法:

方式一(推荐)

  function func1(){
        alert(123);
        return 8
    }

    var ret = func1()
    alert(ret)

方式二:var func2 = new function("参数1", "参数n","函数体");

    var add = new function("a", "b", "alert(a+b)")
    add(1,2);
    alert(add.length)

arguments 对象

获得参数的个数

  var ret=0;
  function add(){
    alert(arguments.length)
  }
  add(1,2,3,4,5)

获得参数的个数和值的总额

    var ret = 0;
    function add(){
        console.log(arguments.length);
        console.log(arguments);

        for (var i in arguments){
            ret += arguments[i]
        }
        return ret;
    }

    alert(add(1,2,3,4))

控制抛出异常

    function func2(){
        if (arguments.length != 3){
            throw new error("param should be 3")
        }
    }

    func2(1,2,3,4)

自执行函数

    (function(arg){console.log(arg);})('123')

instanceof

用于判断一个变量是否某个对象的实例

    var s = "hello";
    var i = 8;

    alert(typeof(s));
    alert(typeof(i));

    var s2 = new string("hello2");
    alert(typeof(s2))
    alert(s2 instanceof string);

    var n = new number(2);
    alert(typeof(n))
    alert(n instanceof number);

在javascript中除了null和undefined以外其他的数据类型都被定义成了对象,也可以用创建对象的方法定义变量,string、math、array、date、regexp都是javascript中重要的内置对象,在javascript程序大多数功能都是通过对象实现的

javascript 有 11 种内置对象, 包括:
array ,string , date, math, boolean, number function, global, error, regexp , object

string对象

创建字符串对象:

    var s = "hello";
    var s2 = new string("hello2");

    alert(s);
    alert(s2);

// string 对象 length 方法
    alert(s.length);

// 遍历字符串
    for (var i in s){
        console.log(s[i])
    }

各种方法

// 用于把字符串显示为斜体
    document.write(s.italics());

// 用于把字符串显示为粗体
    document.write(s.bold());

// 用于创建 html 锚
    document.write(s.anchor("klvchen"));

// 用于把字符串转化成大写
    console.log(s.touppercase());

// 用于把字符串转化成小写
    console.log(s.tolowercase());

// charat返回index位置的字符
    console.log(s.charat(3));

// charcodeat返回index位置的unicode编码
     console.log(s.charcodeat(3));

// search返回匹配字符串的首字符位置索引
    console.log(s.search("l"));

// match返回匹配字符串的数组,如果没有匹配则返回null
    console.log(s.match("l"));

    console.log(s.match("l")[0]);        // 取数组里面的值
    console.log(s.match("l")[1]);

// 替换子字符串
    console.log(s.replace("e","e"));

// 分割字符串
    console.log(s.split("e"));

// 连接字符串
    console.log(s.concat(" world"));

// 截取子字符串,右不包括
    console.log(s.substr(1, 1));
    console.log(s.substring(1, 4));
    console.log(s.slice(1, -1));

// 返回指定第一个元素的位置
    console.log(s.indexof("l"));

array对象

创建数组对象

// 方法一:
    var arr = [1,2,3,4];

// 方法二:
    var arr2 = new array(1,2,3,4);
    //var arr2 = new array(5, "hello", true, [1,2]);

// 输出数组对象的长度
    console.log(arr.length);
    console.log(arr2.length);


// 定义一个长度为3的数组,数组的值因未定义则默认为空
    var arr4 = new array(3);
    console.log(arr4[0]);
    console.log(arr4[1]);
    console.log(arr4[2]);

// 数组是可变成的
    var arr5 = new array(3);
    arr5[5] = 10;
    console.log(arr5.length);

// 二维数组
  var arr6 = new array(6, "klvchen", true, [1,2]);
  alert(arr6[3][0]);

// 连接数组-join方法
  ret = ["hello", "world"].join(" &&& ");
  alert(ret);

// push pop这两个方法模拟的是一个栈操作, push 压栈, pop弹栈   
  var arr7 = [1, 4, 6];
  arr7.push(13);
  console.log(arr7);
  var ret = arr7.pop();
  alert(ret);

// unshift shift。unshift是将value值插入到数组x的开始, shift是将数组x的第一个元素删除
  var arr7 = [1, 4, 6];
  arr7.unshift(45);
  console.log(arr7);
  arr7.shift();
  console.log(arr7);

// 数组排序。reverse 作用为颠倒数组元素;sort 作用为排序数组元素
    var arr8 = [11, 5, 3, 7, 100];
    arr8.reverse();
    console.log(arr8);
    console.log(arr8.sort());

// 按照从小到大排序
    var arr8 = [11, 5, 3, 7, 100];

    function mysort(a,b){
        if (a>b){
            return 1;
        }else if(a<b){
            return -1;
        }else{
            return 0;
        }
    }

    function mysort2(a,b){
        return a-b;
    }

    console.log(arr8.sort(mysort));
    console.log(arr8.sort(mysort2));

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网