json对象,严格模式,字符串的定义和创建,字符串常见的API和属性

75 阅读2分钟

json对象

json对象:描述数据的一种格式,将若干繁杂的属性封装为一个整体
可以直接通过json对象,操作各个属性
1.定义,由{}括起来的键值对,每两个键值对用逗号分开
    key:value
注意事项:所有的key请用双引号括起来
     var stu = {
         "name": "蔡徐坤",
         "age": 18,
         "id": 9527,
         "hobby": ["唱", "跳", "rap", "篮球"],
         "eat": function() {
             console.log("eat")
         },
         "study": function() {
             console.log("study");
         }
     }
     console.log(stu);
2.属性的访问  常用
    a.通过点运算符访问
    对象名.属性名
         console.log(stu.name);
         stu.name = "凢凢";
         console.log(stu.name);
         console.log(stu.age, stu.id);
         for (var i = 0; i < stu.hobby.length; i++) {
             console.log(stu.hobby[i]);
         }
         stu.eat();
         stu.study();
    b.下标法
     对象名[key]
             console.log(stu["name"]);
         var str = "age";
         console.log(stu[str]);
         console.log(stu["id"]);
         for (var i = 0; i < stu.hobby.length; i++) {
             console.log(stu["hobby"][i]);
         }
         stu["eat"]();
         stu["study"]();   
    c.为json对象添加新的自定义属性
     对象名.新属性名 = 属性值
         var stu = {
             "name": "老王",
             "age": 88,
             "score": 100
         }
​
         stu.a = 666;
​
         console.log(stu);
​
    d.json对象的遍历
        每次循环式=时变量代表索引
         for(var 变量 in json对象){
             循环体
         }
         var stu = {
             "name": "老王",
             "age": 88,
             "score": 100
         }
​
         for (var index in stu) {
             console.log(index);
             // forin中不能用点运算符
             // console.log(stu.index);
             console.log(stu[index]);
         }
    e.在一个成员方法中使用其他的成员,需要添加前缀this
        var stu = {
            "name": "老王",
            "age": 88,
            "score": 100,
            "eat": function() {
                console.log("eat");
            },
            "showValue": function() {
                console.log(this.name, this.age, this.socre);
                this.eat();
            }
        }
​
        stu.showValue();

严格模式

严格模式:你必须使用定义过的变量"use strict"修饰的作用域,所有变量必须定义才能使用

indexOf

数组的函数
indexOf
功能:查找目标元素
参数:indexOf(目标元素)
返回值:找到返回下标,找不到返回-1
​
var arr = [5, 6, 4, 7, 8, 3, 9];
var x = arr.indexOf(18);
console.log(x);
​
--------------------------------
数组去重
var arr = [5, 5, 5, 5, 5, 5, 6, 4, 5, 7, 2, 4, 7, 8, 3, 9];
var arr1 = [];
​
for (var i = 0; i < arr.length; i++) {
    if (arr1.indexOf(arr[i]) == -1) {
        arr1.push(arr[i]);
    }
}
​
console.log(arr1);

回调函数

回调函数: 一个被当做参数的函数
为什么需要回调函数
场景:
需要使用别人提供的功能模块,但别人的功能模块不能完全实现自己的需求
需要在别人的模块中调用自己的函数
但是我们无法打开第三行模块的函数体,不能直接修改第三方的代码
​
function f1() {//f1是第三方模块
    f2();      //f2是自己写的模块
}
​
function f2() {
    console.log("真开心嘿嘿嘿");
}
​
f1();
--------------------
function f1(f) { //f==f2
    f(); // f()==f2()
}
​
var f2 = function() {
    console.log("今天下雨了嘿嘿嘿");
}
​
f1(f2);
​
------------------------------------
循环定时器:周期性执行回调函数的功能
setInterval(回调函数,时间间隔)
var count = 0;
var fun = function() {
    console.log(++count);
}
​
setInterval(fun, 1000);

字符串的定义和创建

创建的方法
    a.字面量
    var str = "hello";
    console.log(str);
    console.log(typeof str);
​
    b.构造方法
    var str1 = new String("world");
    console.log(str1);
    console.log(typeof str1);

ASCII码表

asc码:每个字符都对应一个数字,这个数字就是asc码
    字符和对应的asc码是无条件等价的
​
65 'A'
97 'a'
48 '0'
32 空格
13 回车
​
//console.log('c' > 'b' ? "heihei" : "haha");
​
length:字符串的长度
    var str = "hello";
    console.log(str.length);
    str = "aworld";
​
charAt(索引):返回索引对应的字符
    console.log(str.charAt(0));
​
charCodeAt(索引):返回索引对应的字符的asc码值
    console.log(str.charCodeAt(0));
​
-------------------------------------------
统计一个字符的大写字母,小写字母,数字,空格和其他字符的个数
    var str = "12a45bcA   AB+#$%^";
    function fun(str) {
        var bigChar = 0;
        var smallChar = 0;
        var num = 0;
        var space = 0;
        var other = 0;
​
        for (var i = 0; i < str.length; i++) {
            if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
                num++;
            } else if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {
                smallChar++;
            } else if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {
                bigChar++;
            } else if (str.charAt(i) == " ") {
                space++;
            } else {
                other++;
            }
        }
        //           4       3          3      3      5
        console.log(num, smallChar, bigChar, space, other);
    }
​
    fun(str);

字符串常见的API和属性

length: 字符串的长度
charAt(索引):返回索引对应的字符
charCodeAt(索引):返回索引对应的字符的asc码值
​
fromCharCode
    功能:返回asc码对应的字符
    参数:fromCharCode(asc1,asc2...)
    返回值:asc码对应的字符
    注意事项:该方法直接通过类名String调用
        var str = String.fromCharCode(97, 98);
        console.log(str);
​
indexOf("字符串"):查找字符串出现的位置,找到返回下标,找不到返回-1
    只能返回第一次的字符串下标
        var str = "hello world";
        console.log(str.indexOf("o"));
​
lastIndexOf("abc"):查找字符串最后一次出现的位置  如果没找到  返回-1
        var str = "hello world";
        console.log(str.lastIndexOf("o"));
​
replace
    功能:替换,用参数2替换参数1
    参数:replace(被替换字符串,替换的字符串)
    返回值:被替换的字符串
    只能替换一次
        var str = "hello world";
        str = str.replace("o", "z");
        console.log(str);
​
截取字符串
slice
substring
    参数:[起始位置,结束为止) 左闭右开
        var str = "helloworld";
        console.log(str.slice(2, 5));
        console.log(str.substring(2, 5));
​
split
    功能:将字符串按分隔符转为数组
    参数:split(分隔符)
    返回值:一个字符串数组
         var str = "wo shi ge bi de lao wang";
        var arr = str.split(" ");
        console.log(arr);
​
字符串转大写
    console.log("HeiHei".toUpperCase());
字符串转小写
    console.log("HeiHei".toLowerCase());