什么是內(nèi)置對象?
使用MDN查詢方法如何使用?
學(xué)習(xí)Math對象的3-4個方法?
學(xué)習(xí)Date對象的3-4個方法?
學(xué)習(xí)書寫一個函數(shù)格式化日期?
學(xué)習(xí)書寫一個函數(shù)計(jì)算時間差?
一、基本數(shù)據(jù)類型 與 引用數(shù)據(jù)類型的區(qū)別
1.1
-
基本數(shù)據(jù)類型
? 指的是簡單的數(shù)據(jù)類型,有數(shù)字Number、字符串String、布爾Boolean、未定義Undefined、空Null。
-
引用數(shù)據(jù)類型
? 指的是復(fù)雜的數(shù)據(jù)類型,有數(shù)組Array、函數(shù)Function、對象等。
1.2 內(nèi)存中的棧和堆
var num = 123; //基本數(shù)據(jù)類型
var a = num;
a = 456;
console.log(num); //num?
var arr = [11,22,33,44]; //引入數(shù)據(jù)類型
var arr2 = arr;
arr2[0] = '----';
console.log(arr[0]); //arr[0]?
-
棧和堆
- 內(nèi)存可以分為棧區(qū) 和 堆區(qū)
- 棧區(qū):用來存儲用var關(guān)鍵字創(chuàng)建的變量名 和 基本類型的數(shù)據(jù)。
- 堆區(qū):用來存儲引用類型的數(shù)據(jù)。
二.內(nèi)置對象
2.1
-
思考?
內(nèi)置對象、宿主對象和自定義對象的區(qū)別?
-
內(nèi)置對象
系統(tǒng)所提供的對象如:Object、Array、Math、Date等等。
-
宿主對象
JS所運(yùn)行的環(huán)境提供的對象比如:BOM中的Window、DOM中的document;
-
自定義對象
自定義構(gòu)造函數(shù)所創(chuàng)建的對象。
2.2 學(xué)習(xí)內(nèi)置對象
-
手冊
W3C在線或離線手冊
-
如何學(xué)習(xí)一個對象中的方法?
- 方法中功能
- 方法的參數(shù)和類型
- 方法的返回值
- demo
2.3 Math對象
-
Math對象介紹
? Math本身就是一個對象,該對象中集合了很多關(guān)于數(shù)學(xué)運(yùn)算的方法。也就是說,對于后期的一些復(fù)雜一些的數(shù)學(xué)運(yùn)算,不需要自己動手去運(yùn)算,直接調(diào)用Math對象中的方法實(shí)現(xiàn)即可
-
Math對象常用的方法
- Math.abs(數(shù)字); 獲取一個數(shù)字的絕對對象
- Math.round(數(shù)字); 四舍五入
- Math.PI; π
- Math.ceil(數(shù)字); 向上取整
- Math.floor(數(shù)字); 向下取整
- Math.random(); 隨機(jī)數(shù)[0,1);
- Math.max(數(shù)字,數(shù)字,數(shù)字...); 求最大數(shù)
- Math.min(數(shù)字,數(shù)字,數(shù)字...); 求最小數(shù)
2.4 Date類型對象
-
Date類型對象介紹
- Date類型對象是JavaScript提供的日期和時間的操作接口。它可以表示的時間范圍是,1970年1月1日00:00:00前后的各1億天(單位為毫秒)。
- 類型:Date
- 創(chuàng)建日期對象的方式:
<pre class="md-fences md-end-block" lang="javascript" contenteditable="false" cid="n177" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; display: block; break-inside: avoid; text-align: left; background-image: ; background-position: var(--code-block-bg-color); background-size: ; background-repeat: var(--code-block-bg-color); background-attachment: ; background-origin: ; background-clip: ; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;"> new Date(); //當(dāng)前時間 ? new Date(value); //value,傳入的毫秒 ? new Date(dateString); //字符串 ? new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);// 數(shù)字</pre> -
Date類型對象常用的方法
- 獲取設(shè)置年月日
<pre class="md-fences md-end-block" lang="javascript" contenteditable="false" cid="n186" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; display: block; break-inside: avoid; text-align: left; background-image: ; background-position: var(--code-block-bg-color); background-size: ; background-repeat: var(--code-block-bg-color); background-attachment: ; background-origin: ; background-clip: ; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;"> getFullYear(從Date對象以四位數(shù)字返回年份) / setFullYear(設(shè)置年份四位數(shù)字) 年 <script type="text/javascript"> ? var d = new Date() document.write(d.getFullYear()) ? </script> //2017 ? <script type="text/javascript"> ? var d = new Date() d.setFullYear(1992) document.write(d) ? </script> //Web Nov 18 1992 01:19:12 GMT+0800 ? getMonth(獲取Date的月份) / setMonth(設(shè)置Date月份數(shù)字) 月 注意:獲取月份是從0開始的 ? getDate(獲取Date的日期) / setDate(設(shè)置Date的日期數(shù)字) //日</pre>- 獲取星期幾
<pre class="md-fences md-end-block" lang="javascript" contenteditable="false" cid="n191" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; display: block; break-inside: avoid; text-align: left; background-image: ; background-position: var(--code-block-bg-color); background-size: ; background-repeat: var(--code-block-bg-color); background-attachment: ; background-origin: ; background-clip: ; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;"> getDay(); //0-6(周日0到周六6)</pre>- 獲取設(shè)置時分秒毫秒
<pre class="md-fences md-end-block" lang="javascript" contenteditable="false" cid="n195" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; display: block; break-inside: avoid; text-align: left; background-image: ; background-position: var(--code-block-bg-color); background-size: ; background-repeat: var(--code-block-bg-color); background-attachment: ; background-origin: ; background-clip: ; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;"> getHours() / setHours(數(shù)字) 時 ? getMinutes() / setMinutes(數(shù)字)分 ? getSeconds() / setMinutes(數(shù)字) 秒 ? getMilliseconds() / setMilliseconds(數(shù)字) 毫秒</pre>- 獲取設(shè)置毫秒1970年1月1日至今的毫秒
<pre class="md-fences md-end-block" lang="javascript" contenteditable="false" cid="n199" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; display: block; break-inside: avoid; text-align: left; background-image: ; background-position: var(--code-block-bg-color); background-size: ; background-repeat: var(--code-block-bg-color); background-attachment: ; background-origin: ; background-clip: ; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;"> getTime(); / setTime(數(shù)字);</pre>