03JavaScript程序设计修炼之道 2019-06-06_20-10-17_2019-06-06_21-12-50 对象定义方式:{}、构造;字符串及常用方法;

29object.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        // 描述一个人的信息
        var name = "along";
        var sex = "M";
        var age = 32;
        //  liwei
        var name  = "liwei";
        var sex2 = "M";
        var age2 = 33; 
        console.log(name);
        // 对象  具体的事物或者抽象的事件
        // 对象定义 无序属性的集合
        /*  键值对 直接量
            var obj = {
                k:v,
                k:v
                ...
            }        
        */
        var p1 = {
            name:"along", // 属性------特征
            sex: "M",
            age: 32,
            teach: function() { // 方法-----动态
                console.log("hard workiong");
            }
        };
        var p2 = {
            name:"liwei", // 属性------特征
            sex: "M",
            age: 33
        }
        console.log(p1,typeof p1); // object
        console.log(p2);
        // 访问对象的成员 对象.成员 
        console.log(p1.age);
        console.log(p1['age']);
        var k = 'sex';
        console.log(p1[k]); // p1['sex'] p1.k
        p1.teach();
        
        // 对象可以动态添加属性
        p1.tel = "13988888888";
        console.log(p1);
        p1.sing = function() {
            console.log("along is singing");
        }
        p1.sing(); // window.sing()


        function fn() {

        }

        fn(); // window.fn();
    </script>
</body>
</html>

30object.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        // 对象的创建方式二 通过new 构造函数 Array String Number Date Function Object... 
        var arr = new Array(1,2);
        /* arr也是对象 可以动态添加属性
        arr.index = 10;
        console.log(arr.index);
        */
        /*
        var num = 10;// 基本数据类型 不能添加属性
        num.index = 10;
        console.log(num.index);
        */
        var num = new Number(10);
        num.index = 10;
        console.log(num.index);

        var str = new String("hello");// 字符串对象

        function fn() {

        } 
        fn.a = 10;
        console.log(fn.a); 
    </script>
</body>
</html>

31object.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        var num = 10;
        var num2 = num;// num2 = 10
        num = 20;
        console.log(num2); // 10 
        
        // 引用类型
        var obj = {name: "along"};
        var obj2 = obj;
        obj.name = "along666";
        console.log(obj2.name);

        var arr = [10,20,30];
        var brr = arr;
        brr[0] = 100;
        console.log(arr[0]);

        // arr [100,20,30]

        function fn(brr) {
            brr[0]--;
        }
        fn(arr);  
        console.log(arr[0]); // 99
    </script>
</body>
</html>

32string.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        // 
        /*
        var str = "hello";
        var str2 = '123';
        */
        function fnCharAt() {
            var str = "aBg0hdhadjk123";
            var ch = str.charAt(0); // 返回对应位置的字符
            var code = str.charCodeAt(3);// 返回对应位置的字符编码  a 97  A 65 0 48
            var ch2 = String.fromCharCode(99); // 返回编码对应的字符
            console.log(ch);
            console.log(code);
            console.log(ch2);
        }
        fnCharAt();


        function fnIndexOf() {
            var str = "heillwoer23kk";
            var index = str.lastIndexOf('2');
            console.log(index);
        }
        fnIndexOf();


        function fnSubstr() {
            var str = "welcome to china";
            var res = str.substr(3);// 从序号3开始 截取到最后
            var res2 = str.substr(3,7);// 从序号3开始 截取7个长度字符
            var res3 = str.substring(3);// // 从序号3开始 截取到最后
            var res4 = str.substring(3,7); // 从序号3开始 截取到序号为7的前一位为止
            var res5 = str.slice(3);// 从序号3开始 截取到最后
            var res6 = str.slice(3,7);// 从序号3开始 截取到序号为7的前一位为止
            console.log(res); // come to china
            console.log(res2);// come to
            console.log(res3); // come to china
            console.log(res4); // come
            console.log(res6);
            
        }
        fnSubstr();

        
        function fnSplit() {
            var str = "welcome to china";
            var arr = str.split(" ");
            str = arr.join(" ");
            console.log(arr,str);
            var filename = "1.4.2.txt";
            var res = filename.split(".")
            var extname = res[res.length-1];
            console.log(extname);
            // 1 "http://www.baidu.com?uname=zs&age=22" 如何获取uname和age  
            // 2 "abcdefg" => "gfedcba"  
        }
        fnSplit()

        function fnReplace() {
            var str = "nnd 大家好 nnd 我是";
            //str = str.replace('nnd',"***"); // 惰性
            console.log(str.split("nnd"));// ["", " 大。。。"," 我是"]
            str = str.split("nnd").join("*");
            console.log(str);
        }
        fnReplace();
        
    </script>
</body>
</html>

 

 

 

猜你喜欢

转载自www.cnblogs.com/HiJackykun/p/11141131.html