JS(四):js的字符串对象、数组对象、函数对象

1.首先,了解一下JS的11种内置对象是哪些?

Array:数组对象

String:字符串对象

Date:日期对象

Math:Math对象

Boolean:布尔值对象

Number:数字对象

Global:全局对象

Error:错误对象

RegExp:正则对象

Object:对象对象

Function:函数对象

2.String对象

2.1创建字符串的两种方式

//1.
var str1="hello";
//2.
var str2=new String("hello2");
console.log(typeof str1)//------->String
console.log(typeof str2)//------->Object
//字符串的属性
console.log(str1.length)//---------->5

 

2.2字符串的方法

//1.编排方法(一般不用)
console.log(str1.italics())//------->  <i>hello</i>
console.log(str1.bold())//------->  <b>hello</b>
console.log(str1.anchor())//------->  <a name="undefined">hello</a>
//————————————————————————————————————————————————————————————————————————————————————————————————————————
//2.大小写转换
console.log(str1.toLowerCase())//变成小写
console.log(str1.toUpperCase())//变成大写
//————————————————————————————————————————————————————————————————————————————————————————————————————————
//3.查询字符串索引
var str3="welcome to the world of JS!";
var str4=str3.indexOf("l");//从字符串开始进行查询第一个
var str5=str3.lastIndexOf("l");//从字符串进行查询最后一个
alert(str4);//-----------> 2
alert(str5);//-----------> 18
//————————————————————————————————————————————————————————————————————————————————————————————————————————
//4.对子字符串的处理---截取字符串
var str6="welcome to the world of JS!";
console.log(str6.substr(1,3));//从索引位置1开始取,取3个字母---》elc
console.log(str6.substring(1,3));//从索引位置1开始取,取到索引位置3,但是遵循:左取右不取;-->el
//————————————————————————————————————————————————————————————————————————————————————————————————————————
//5.字符串切片(重)
var str7="welcome to the world of JS!";
console.log(str7.slice(1,4));//-------->elc
console.log(str7.slice(-3,-1));//-------->JS
//————————————————————————————————————————————————————————————————————————————————————————————————————————
//6.字符串替换
var str8="welcome to the world of JS!";
console.log(str8.replace("welcome","oh!"));//把welcome替换成oh!,——>oh! to the world of JS!

二、数组对象

 

说明:length;jion方法;concat方法;reverse:反转顺序;sort:按照最高位大小排序;自定义函数:按照大小排序;删除或插入数组的数据:splice;

push;pop;shift;unshift

//数组的创建方式:
    //1.
    var arr1=[1,"hello",[11,2],{"name":"yuan"}];
    //2.
    //var arr0=new Array(size);//size参数是数组,表示数组长度;若不填,则该数组是长度不限制
    var arr0=new Array(6);//
    var arr00=new Array(1,"WORD",true,[1,2,3]);
    console.log(typeof arr1);//---->object
    console.log(typeof arr0);//---->object
    console.log(typeof arr00);//---->object
//————————————————————————————————————————————————————————————————————————————————————————————————————————
//获取数组对象的个数:length
    var arr2=new Array(1,12,123,1234,12345);
    console.log(arr2.length);//--------->5
//————————————————————————————————————————————————————————————————————————————————————————————————————————
//数组对象的方法
    //连接数据-jion方法:
    var arr3=new Array("a","b","c");
    console.log(arr3.join("888"));//-----> a888b888c
//————————————————————————————————————————————————————————————————————————————————————————————————————————
//concat方法:嫁接
    var arr4=new Array(1,2,3);
    console.log(arr4.concat("abc","df"));//-------> [1, 2, 3, "abc","df"]
//————————————————————————————————————————————————————————————————————————————————————————————————————————
//reverse:反转顺序
    var arr5=[23,45,37,88];
    console.log(arr5.reverse());//------> [88, 37, 45, 23]
//————————————————————————————————————————————————————————————————————————————————————————————————————————
//sort:按照最高位大小排序
    var arr6=[12,34,23,14];
    console.log(arr6.sort());//-------->[12, 14, 23, 34]
    var arr7=[12,100,23,14];
    console.log(arr7.sort());//-------->[100, 12, 14, 23]
//————————————————————————————————————————————————————————————————————————————————————————————————————————
//自定义函数:按照大小排序
    var arr8=[12,100,23,14];
    function f(a,b) {
        if(a>b){
            return 1//只要这里是正数就行
        }else if(a<b){
            return -1//只要这里是负数就行
        }else if(a==b){
            return 0
        }
    }//这个函数可以简写为下面的;
    function f1(a,b) {
        return a-b
    }
    console.log(arr8.sort(f));//-------->[12, 14, 23, 100]
//————————————————————————————————————————————————————————————————————————————————————————————————————————
//删除或插入数组的数据:splice
     //splice(开始索引位置,删除个数,新增的values……)
    var arr9=[1,2,3,4,5,6,7,8,9,0];
    console.log(arr9.splice(1,2));//从数组索引1位置开始截取2个值----->[2, 3]
    console.log(arr9);//------->[1, 4, 5, 6, 7, 8, 9, 0]
    var arr10=[1,2,3,4,5,6,7,8,9,0];
    arr10.splice(0,0,"a","b","c");//从0位置删除0个数,插入"a","b","c"
    console.log(arr10);//---》["a", "b", "c", 1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
//————————————————————————————————————————————————————————————————————————————————————————————————————————
//(重)数组的进出栈操作
//   push  :栈操作:从原数据的后面顺序,塞入数据;
    var arr11=[1,2,3];
    arr11.push([7,8,0]);//注意:arr11.push(7,8,0)是不一样的,后面的是一个个的塞入,前面的是一个整体塞入
    arr11.push("hello")
    console.log(arr11);//----》[1, 2, 3, [7, 8, 0], "hello"]
    console.log(arr11.length)
// pop  :栈操作 取出数据;(继上面的push ,规则:先进入的后出来)
    console.log(arr11.pop());//----》hello
    console.log(arr11.pop());//----》[7, 8, 0]
//————————————————————————————————————————————————————————————————————————————————————————————————————————
//unshift操作:插入数据
    var arr12=[4,5,6];
    arr12.unshift([11,22])//从原数据的前面插入数据;
    console.log(arr12)//----》[[11, 22], 4, 5, 6]
    console.log(arr12.length)
    arr12.unshift(true)
    arr12.unshift("yes")
    console.log(arr12)//----》["yes", true, [11, 22], 4, 5, 6]
//shift操作:移除数据,同样:先进入的后出来
    arr12.shift();
    console.log(arr12);//------》[true, [11, 22], 4, 5, 6]

 

三、函数对象

函数的定义、参数、length属性、arguments、匿名函数

//函数的创建方式一:
    function f(x,y) {
        alert(123);
        return x+y;
    }
    console.log(f(23,5678));
//函数的创建方式二(这种方式不用,只是告诉你,函数是一个对象):
    var  obj= new Function("name","console.log(\"hello\"+name)");
    obj("张三")//-----》hello张三
//————————————————————————————————————————————————————————————————————————————————————————————————————————
//JS的函数调用
// 没有编写的先后顺序,执行顺序是:先加载了所以function,再执行代码;
    //即:前面可以调用后面定义的function()
//参数个数如果多传了,也不会报错,只会传入需要的:
    function add(x,y,z) {
        return x+y+z;
    }
    console.log(add(1,2,3,4,5,6,7))//传了这么多参数,而函数只取前面三个----》6
    console.log(add(1,2))//少传了,最后一个参数没有,就是undefined,转为数字失败,就是NaN
//————————————————————————————————————————————————————————————————————————————————————————————————————————
//函数的length属性:指的是参数的长度
    function f1(x,y) {
        alert(123);
        return x+y;
    }
    console.log(f1.length)//指的是参数的长度-----》2
//————————————————————————————————————————————————————————————————————————————————————————————————————————
//arguments:函数内置对象,表示可以容纳所有的函数参数
    function f() {
        var sum=0;
        for (var i=0;i<arguments.length;i++){
            sum+=arguments[i]
        }
        return sum;
    }
    console.log(f(1,2,3,4,5,6,7,8,9,0))
//————————————————————————————————————————————————————————————————————————————————————————————————————————
//匿名函数:匿名函数的调用必须先定义,再调用;而不像一般js的函数,没有顺序;
    var func =function(arg){
        alert(arg)
    };
        func("hello");
//也可这样:
    (function (arg) {
        alert(arg)
    })("hel")

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冷凝娇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值