JavaScript中的Date,RegExp,Function物件

北京-IT拾荒者發表於2018-06-28

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 返回值;
}
複製程式碼

功能說明:

可以使用變數、常量或表示式作為函式呼叫的引數

函式由關鍵字function定義

函式名的定義規則與識別符號一致,大小寫是敏感的

返回值必須使用return

Function 類可以表示開發者定義的任何函式。

用 Function 類直接建立函式的語法如下:

function 函式名 (引數){

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

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

例項:

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')
複製程式碼

JavaScript中的Date,RegExp,Function物件

識別圖中二維碼,領取pyhton全套視訊資料

相關文章