JavaScript 对象

JavaScript中除了null和undefined以外其他的数据类型都被定义成了对象,也可以用创建对象的方法定义变量,String、Math、Array、Date、RegExp都是JavaScript中重要的内置对象,在JavaScript程序大多数功能都是基于对象实现的。

字符串对象

字符串对象创建

字符串创建(两种方式)
① 变量 = “字符串”
② 字串对象名称 = new String (字符串)

var str1="hello world";
var str1= new String("hello word");

上面创建的字符串是一个字符串对象

String {0: "h", 1: "e", 2: "l", 3: "l", 4: "o", 5: " ", 6: "w", 7: "o", 8: "r", 9: "l", 10: "d", length: 11, [[PrimitiveValue]]: "hello world"}0: "h"1: "e"2: "l"3: "l"4: "o"5: " "6: "w"7: "o"8: "r"9: "l"10: "d"length: 11__proto__: String[[PrimitiveValue]]: "hello world"

字符串对象的属性和函数

字符串对象的属性和函数

 x.length         ----获取字符串的长度
 x.toLowerCase()        ----转为小写
 x.toUpperCase()        ----转为大写
 x.trim()               ----去除字符串两边空格    

字符串查询方法

  • x.charAt(index) ----str1.charAt(index);----获取指定位置字符,其中index为要获取的字符索引

  • x.indexOf(findstr,index)----查询字符串位置 index是查询的初始位置

  • x.lastIndexOf(findstr) --查询的是字符串的索引位置

  • x.match(regexp) ----match返回匹配字符串的数组,如果没有匹配则返回null

  • x.search(regexp) ----search返回匹配字符串的首字符位置索引

var str1="welcome to the world of JS!";
            var str2=str1.match("world");
            var str3=str1.search("world");
            alert(str2[0]);  // 结果为"world"
            alert(str3);     // 结果为15  位置

字符串处理方法

截断:
substr(位置,长度)
substring(开始位置,结束位置) 顾头不顾尾

slice(start,结束位置) 顾头不顾尾 切片操作

  • x.substr(start, length) ----start表示开始位置,length表示截取长度

  • x.substring(start, end) ----end是结束位置

  • x.slice(start, end) ----切片操作字符串

  • x.replace(findstr,tostr) ---- 字符串替换

  • x.split(); ----分割字符串

  • x.concat(addstr) ---- 拼接字符串

 var str1="abcdefgh";
        var str2=str1.slice(2,4);
        var str3=str1.slice(4);
        var str4=str1.slice(2,-1);
        var str5=str1.slice(-3,-1);

        alert(str2); //结果为"cd"

        alert(str3); //结果为"efgh"

        alert(str4); //结果为"cdefg"

        alert(str5); //结果为"fg"
var str1="一,二,三,四,五,六,日";
             var strArray=str1.split(",");
               console.log(strArray)
Python re 找索引的位置span方法

数组对象

创建方式

  • 创建方式1:
    var arrname = [元素0,元素1,….]; // var arr=[1,2,3];

  • 创建方式2:
    var arrname = new Array(元素0,元素1,….); // var test=new Array(100,"a",true);

数组对象的属性和方法

  • join方法 x.join(bystr) ----将数组元素拼接成字符串
var arr1=[1, 2, 3, 4, 5, 6, 7];
    var str1=arr1.join("-");
    alert(str1);  //结果为"1-2-3-4-5-6-7" 

数据增加元素

console.log(arr1.join("=="));
console.log(arr1.concat(1,2,3));//并没有改变原来的
console.log(arr1.length);
console.log(arr1.concat([1,2,3]));//一次性添加
console.log(arr1.length);
console.log(arr1.toString());//转换成字符串传送

数组排序

倒序

var arr1 =[54,32,45,100];
        arr1.reverse()
        console.log(arr1);
        arr1.sort();//这个是按照第一个数字的ASCII排序
         console.log(arr1);

//自定义一个数字的大小排序

function intsort(a,b){
    return a-b;//  返回值大于0 就是正数  否则是负数
}
console.log(arr1.sort(intsort));//排序

数组的切片

var arr1 =[54,32,45,100,500,55];
console.log(arr1.slice(1,4));
console.log(arr1)

结果:
[32, 45, 100] 这是切出来的

数组的删除

splice

//x. splice(start, deleteCount, value, ...)

//使用注解

//x代表数组对象
//splice的主要用途是对数组指定位置进行删除和插入
//start表示开始位置索引
//deleteCount删除数组元素的个数
//value表示在删除位置插入的数组元素
//value参数可以省略       
        
var a = [1,2,3,4,5,6,7,8];
a.splice(1,2);

alert(a.toString());//a变为 [1,4,5,6,7,8]

a.splice(1,1);

alert(a.toString());//a变为[1,5,6,7,8]

a.splice(1,0,2,3);

alert(a.toString());//a变为[1,2,3,5,6,7,8]

遍历数组

var arr1 = [111,222,333];
//        for (var i in arr1){
//            console.log(arr1[i])
//        }
        for (i=0;i<arr1.length;i++){
            console.log(arr1[i])
        }

数组的push和pop shift unshift

//栈操作1

var arr1=[1,2,3];
arr1.push(99);//压到最后
console.log(arr1);
arr1.pop();//后进先出
console.log(arr1);

// 栈操作2shift unshift

var arr1=[1,2,3];
arr1.unshift(123132);//放到了最前面
console.log(arr1);
arr1.shift();//先进后出 ==后进先出
console.log(arr1);

都是后进先出的方法,不同在于unshift压栈的时候放在最前面

自定义的字典对象

key的引号可有可无,最后都会去掉,value字符串的时候双引号
//json实际就是Javascript的对象
容错率高,不能

Date对象

打印具体的格式的时间

  • 获取日期和时间
  • getDate() 获取日
  • getDay () 获取星期
  • getMonth () 获取月(0-11)
  • getFullYear () 获取完整年份
  • getYear () 获取年
  • getHours () 获取小时
  • getMinutes () 获取分钟
  • getSeconds () 获取秒
  • getMilliseconds () 获取毫秒
  • getTime () 返回累计毫秒数(从1970/1/1午夜)

Date对象的方法—设置日期和时间n

创建时间对象

//方法1:不指定参数
var nowd1=new Date();
alert(nowd1.toLocaleString( ));
//方法2:参数为日期字符串
var nowd2=new Date("2004/3/20 11:12");
alert(nowd2.toLocaleString( ));
var nowd3=new Date("04/03/20 11:12");
alert(nowd3.toLocaleString( ));
//方法3:参数为毫秒数
var nowd3=new Date(5000);
alert(nowd3.toLocaleString( ));
alert(nowd3.toUTCString());

//方法4:参数为年月日小时分钟秒毫秒
var nowd4=new Date(2004,2,20,11,12,0,300);
alert(nowd4.toLocaleString( ));//毫秒并不直接显示

小练习

var date = new Date();
var year=date.getFullYear();
var month=date.getMonth()+1;//0-11
var day = date.getDate();
var hour=date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var week = date.getDay();

console.log(year+"-"+month+"-"+day+" "+hour+":"+minutes+":"+seconds+" "+parseweek(week));

function parseweek(week) {
    var arry =["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
    return arry[week];
}

结果:
2017-5-24 22:39:31 星期三

Math对象

  • abs(x) 返回数的绝对值。
  • exp(x) 返回 e 的指数。
  • floor(x)对数进行下舍入。
  • log(x) 返回数的自然对数(底为e)。
  • max(x,y) 返回 x 和 y 中的最高值。
  • min(x,y) 返回 x 和 y 中的最低值。
  • pow(x,y) 返回 x 的 y 次幂。
  • random() 返回 0 ~ 1 之间的随机数。
  • round(x) 把数四舍五入为最接近的整数。
  • sin(x) 返回数的正弦。
  • sqrt(x) 返回数的平方根。
  • tan(x) 返回角的正切。

Math.random是取的0-1之间的数
*100
1-100之间的随机数

var num = Math.random();//产生0-1中间的随机数
num=num*100;
num=Math.round(num);//四舍五入
console.log(num);

function对象(重点)

function实际就是类

函数的定义

function 函数名 (参数){<br>    函数体;
    return 返回值;
}

用 Function 类直接创建函数的语法如下:
var 函数名 = new Function("参数1","参数n","function_body");
注意:js的函数加载执行与python不同,它是整体加载完才会执行,所以执行函数放在函数声明上面或下面都可以:

函数function 对象属性

函数的调用

-------------------面试题-----------
 function a(a,b){
    alert(a+b);
}

   var a=1;
   var b=2;
   a(a,b)

这个会报错,因为a已经被覆盖了,后面不能当做函数进行调用了

函数的内置对象argums

使用arguments可以是直接使用函数的属性

function add(a,b){

        console.log(a+b);//3
        console.log(arguments.length);//2
        console.log(arguments);//[1,2]

    }
    add(1,2)

    ------------------arguments的用处1 ------------------
    function nxAdd(){
        var result=0;
        for (var num in arguments){
            result+=arguments[num]
        }
        alert(result)

    }

    nxAdd(1,2,3,4,5)

//     ------------------arguments的用处2 ------------------

    function f(a,b,c){
        if (arguments.length!=3){
            throw new Error("function f called with "+arguments.length+" arguments,but it just need 3 arguments")
        }
        else {
            alert("success!")
        }
    }

    f(1,2,3,4,5)

匿名函数

先想一下Python的lambd的使用

(lambda x,y:x+y)(1,2)
只使用一次,省内存

js版本的匿名函数

(function () {
            console.log("hello")
        })()
(function (name) {
            console.log(name)
        })("haha")

参考内容:

http://www.cnblogs.com/yuanchenqi/articles/6893904.html

原文地址:https://www.cnblogs.com/Python666/p/6901508.html