<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 什么是對象 對象只是一種特殊的數據類型。對象是一組無序的相關屬性和方法組成。 * 屬性:事物的特征,對象里的屬性就是用來表現該對象具備哪些特征 * 方法:事物的行為,對象里的方法用來表示該對象具備哪些行為 # 創建對象的三種方式 * 利用字面量創建對象 * 利用new Object創建對象 * 利用構造函數創建對象 ``` //第一種創建對象的方法 varobj = {}//創建的空對象 varobj = { ???????name:"編程語言", ???????age:18, ?????? sex:"男", ???????wechat:"biancheng", fun:function(){ console.log("使用微信小程序編程"); } } console.log(obj.name); console.log(obj.fun()); //第二種創建對象的方法 var obj = new Object();//創建空對象 obj.name = "編程語言"; obj.age = 18; obj.fun = function(){ console.log("微信小程序編程") } console.log(obj) obj.fun() //第三種創建對象的方法 function Person(name,age,sex,wechat){ this.name = name; this.age = age; this.sex = sex; this.wechat = wechat; this.fun =function(log){ console.log(log) } } var obj = newPerson("編程語言",18,"男","biancheng"); console.log(obj); obj.fun("使用微信小程序編程"); ``` # new關鍵字的執行過程 通過構造函數new一個對象 ``` function Person(name,age) { //創建構造函數Person this.name = name; this.age = age; this.action = function (jineng) { console.log(name + "具備" + jineng + "的技能") } } //創建對象1 var obj1 = new Person('編程語言', 18) ``` 這里在new一個對象出來時會執行下面四件事 * 在電腦內存中創建一個空對象 * 讓構造函數里的this指向一個新的對象 * 執行構造函數里的代碼,給這個新對象添加屬性和方法 * 返回新對象 # 對象的使用 ***** ## **1.訪問對象的屬性** 對象屬性的調用語法有兩種 * 對象名.屬性名 * 對象名['屬性名'] ``` //訪問對象的屬性 var obj = { ??????name:"編程語言", ??????age:18 } console.log(obj.name) console.log(obj['name']) ``` ## **2.訪問對象的方法** 對象中的方法調用就一種方式:對象名.方法名() ``` var obj = { name: '編程語言', age: 18, code:function(){console.log("對象的方法")} } // obj.code() 就是直接調用obj對象里的code()方法 ``` # 內置對象 **** 內置對象就是指JavaScript自帶的一些對象,供開發者使用,這些對象提供了一些常用的功能。開發者可以很方便的使用這些內置對象,不需要關心這些內置對象的實現原理。 常見的內置對象如:Math、Array、Date等 常用的學習文檔如下: * MDN [官方地址](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript) * W3cschool [官方地址](https://www.w3school.com.cn/js/index.asp) * 菜鳥教材 [官方地址](https://www.runoob.com/js/js-tutorial.html) #### **內置對象隨機數** Math.random() 得到一個大于等于0 小于1之間的隨機數;多使用抽獎場景 * 獲取兩數之間的隨機數 (大于等于min小于max) ``` //生成的可能是小數也可能是整數 Math.random() * (max - min) + min console.log(Math.random()*?(10-1)?+1) //獲取隨機整數 //加載頁面觸發事件 onLoad:function(){ //console.log(this.data.list) //console.log(Math.PI) console.log(this.getRandomInt(1,100)) }, // getRandomInt(min,max){ min\=Math.ceil(min); max\=Math.floor(max); //不包括最大值,包含最小值 returnMath.floor(Math.random()?\*?(max\-min)?+min); } ``` ## Date日期對象 Date對象是一個構造函數對象,必須使用new對象來創建需要使用的對象后才能使用。 創建日期對象的四種方法 ``` new Date() new Date(year, month, day, hours, minutes, seconds, milliseconds) new Date(dateString) new Date(milliseconds) ``` ### **獲取日期的方法** | 方法 | 描述 | | --- | --- | | getDate() | 以數值返回天(1-31) | | getDay() | 以數值獲取周名(0-6) | | getFullYear() | 獲取四位的年(yyyy) | | getHours() | 獲取小時(0-23) | | getMilliseconds() | 獲取毫秒(0-999) | | getMinutes() | 獲取分(0-59) | | getMonth() | 獲取月(0-11) | | getSeconds() | 獲取秒(0-59) | | getTime() | 獲取時間(從1970年1月1日至今) | ``` var date = new Date(); console.log(date.getTime())?????//返回1619074784197?從1970年1月1日到現在的時間差,單位毫秒 console.log(date.getFullYear())?//2021 console.log(date.getDay())??????//4 console.log(date.getHours())????//15 console.log(date.getMilliseconds())????//202 console.log(date.getMinutes())????//202 console.log(date.getMonth())??? //3 獲取的月份是從0開始所有獲取的是上個月 console.log(date.getSeconds())????//50 var ymd = date.getFullYear()?+'年'+?(date.getMonth()+1)?+'月'+date.getDate()?+'日'; var his = date.getHours()?+'時'+date.getMinutes()?+'分'+date.getSeconds()?+'秒'; console.log(ymd,his); //2021年4月22日 15時24分55秒 ``` ### **日期設置方法** | 方法 | 描述 | | --- | --- | | setDate() | 以數值(1-31)設置日 | | setFullYear() | 設置年(可選月和日) | | setHours() | 設置小時(0-23) | | setMilliseconds() | 設置毫秒(0-999) | | setMinutes() | 設置分(0-59) | | setMonth() | 設置月(0-11) | | setSeconds() | 設置秒(0-59) | | setTime() | 設置時間(從1970年1月1日至今的毫秒數) | ``` //設置日期 var date = new Date(); date.setFullYear(2022); console.log(date.getFullYear());//2022 ``` ### **倒計時案例** ``` var dateNow = new Date(); //現在的時間 var dateEnd = newDate('2021-04-22?18:00:00'); //設置下午六點為結束時間 var time = dateEnd.getTime() - dateNow.getTime(); ?//現在的時間到結束時間的毫秒數 var minutes = Math.ceil(time/60000); //現在的時間到結束時間的分鐘數(1000毫秒=1秒) var info = parseInt(time/1000/60/60%24)?+'小時'+parseInt(time/1000/60%60)?+'分'+Math.ceil(time/1000%60)?+'秒'; console.log(dateNow); //Thu Apr 22 2021 16:12:58 GMT+0800 (中國標準時間) console.log(info); //1小時47分2秒 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看