JavaScript中的Date,RegExp,Function對象

Date對象

建立Date對象

//方法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( ));
//毫秒並不直接顯示

 

Date對象的方法—獲取日期和時間

獲取日期和時間
getDate()                 獲取日
getDay ()                 獲取星期
getMonth ()               獲取月(0-11)
getFullYear ()            獲取完全年份
getYear ()                獲取年
getHours ()               獲取小時
getMinutes ()             獲取分鐘
getSeconds ()             獲取秒
getMilliseconds ()        獲取毫秒
getTime ()                返回累計毫秒數(從1970/1/1午夜)

 

Date對象的方法—設置日期和時間

//設置日期和時間
//setDate(day_of_month)       設置日
//setMonth (month)                 設置月
//setFullYear (year)               設置年
//setHours (hour)         設置小時
//setMinutes (minute)     設置分鐘
//setSeconds (second)     設置秒
//setMillliseconds (ms)       設置毫秒(0-999)
//setTime (allms)     設置累計毫秒(從1970/1/1午夜)
   
var x=new Date();
x.setFullYear (1997);    //設置年1997
x.setMonth(7);        //設置月7
x.setDate(1);        //設置日1
x.setHours(5);        //設置小時5
x.setMinutes(12);    //設置分鐘12
x.setSeconds(54);    //設置秒54
x.setMilliseconds(230);        //設置毫秒230
document.write(x.toLocaleString( )+"<br>");
//返回1997年8月1日5點12分54秒

x.setTime(870409430000); //設置累計毫秒數
document.write(x.toLocaleString( )+"<br>");
//返回1997年8月1日12點23分50秒

 

Date對象的方法—日期和時間的轉換

日期和時間的轉換:

getTimezoneOffset():8個時區×15度×4分/度=480;
返回本地時間與GMT的時間差,以分鐘爲單位
toUTCString()
返回國際標準時間字符串
toLocalString()
返回本地格式時間字符串
Date.parse(x)
返回累計毫秒數(從1970/1/1午夜到本地時間)
Date.UTC(x)
返回累計毫秒數(從1970/1/1午夜到國際時間)

 

RegExp對象

//RegExp對象
   // 在表單驗證時使用該對象驗證用戶填入的字符串是否符合規則.
   //建立正則對象方式1  參數1 正則表達式  參數2 驗證模式  g global / i 忽略大小寫. //參數2通常填寫g就能夠,也有「gi」.
   // 用戶名 首字母必須是英文, 除了第一位其餘只能是英文數字和_ . 長度最短不能少於6位 最長不能超過12位
   //----------------------------建立方式1
   /* var reg1 = new RegExp("^[a-zA-Z][a-zA-Z0-9_]{5,11}$","g");
   //
   //驗證字符串
   var str = "bc123";
   alert(reg1.test(str));// true
   //----------------------------建立方式2  /填寫正則表達式/匹配模式;
   var reg2 = /^[a-zA-Z][a-zA-Z0-9_]{5,11}$/g;
   alert(reg2.test(str));// true
    */
   //-------------------------------正則對象的方法-------------------
       //test方法  ==>  測試一個字符串是否複合 正則規則. 返回值是true 和false.
   //-------------------------String 中與正則結合的4個方法------------------.
   // macth search split replace
   var str = "hello world";
   //alert(str.match(/o/g)); //查找字符串中 複合正則的 內容.
   //alert(str.search(/h/g));// 0  查找字符串中符合正則表達式的內容位置
   //alert(str.split(/o/g)); // 按照正則表達式對字符串進行切割. 返回數組;
   alert(str.replace(/o/g, "s")); // hells wsrld  對字符串按照正則進行替換.

 

Math對象

//Math對象
   //該對象中的屬性方法 和數學有關.
   //Math是內置對象 , 與Global的不一樣之處是, 在調用時 須要打出 "Math."前綴.
   //屬性學習:
   //alert(Math.PI);
   //方法學習:
       //alert(Math.random()); // 得到隨機數 0~1 不包括1.
       //alert(Math.round(1.5)); // 四捨五入
       //練習:獲取1-100的隨機整數,包括1和100
            //var num=Math.random();
           //num=num*10;
           //num=Math.round(num);
           // alert(num)
       //============max  min=========================
       /* alert(Math.max(1,2));// 2
       alert(Math.min(1,2));// 1 */
       //-------------pow--------------------------------
       alert(Math.pow(2,4));// pow 計算參數1 的參數2 次方.
       
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)    返回角的正切。

 

Function 對象(重點)

函數的定義:

function 函數名 (參數){
函數體;
   return 返回值;
}

 

功能說明:javascript

可使用變量、常量或表達式做爲函數調用的參數php

函數由關鍵字function定義java

函數名的定義規則與標識符一致,大小寫是敏感的python

返回值必須使用return面試

Function 類能夠表示開發者定義的任何函數。正則表達式

用 Function 類直接建立函數的語法以下:數組

function 函數名 (參數){
   
函數體;
  return 返回值;
}
//another way:
var 函數名 = new Function("參數1","參數n","function_body");

 

雖然因爲字符串的關係,第二種形式寫起來有些困難,但有助於理解函數只不過是一種引用類型,它們的行爲與用 Function 類明確建立的函數行爲是相同的。bash

實例:dom

alert(1);
function func1(){
   alert('hello wang!');
   return 8
}

   ret=func1();
   alert(ret)
----------------

var func1=new Function("name","alert(\"hello\"+name);")
func1("wang")

 

注意:js的函數加載執行與python不一樣,它是總體加載完纔會執行,因此執行函數放在函數聲明上面或下面均可以:函數

<script>
   //f(); --->OK
   function f(){
       console.log("hello")
   }
   f() //----->OK
</script>

 

Function 對象的 length 屬性

如前所述,函數屬於引用類型,因此它們也有屬性和方法。

好比,ECMAScript 定義的屬性 length 聲明瞭函數指望的參數個數。

alert(func1.length)

 

Function 對象的方法

Function 對象也有與全部對象共享的 valueOf() 方法和 toString() 方法。這兩個方法返回的都是函數的源代碼,在調試時尤爲有用。

 

函數的調用

function func1(a,b){

   alert(a+b);
}
   func1(1,2);  //3
   func1(1,2,3);//3
   func1(1);    //NaN
   func1();     //NaN

   //只要函數名寫對便可,參數怎麼填都不報錯.

-------------------面試題-----------
function a(a,b){
   alert(a+b);
}
  var a=1;
  var b=2;
  a(a,b)

 

函數的內置對象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)

 

匿名函數

// 匿名函數
   var func = function(arg){
       return "tony";
   }

// 匿名函數的應用
   (function(){
       alert("tony");
   } )()

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

 


 

識別圖中二維碼,領取python全套視頻資料

相關文章
相關標籤/搜索