標(biāo)準(zhǔn)對(duì)象 | toString() | Date | split() | HTTP報(bào)文

一 標(biāo)準(zhǔn)對(duì)象

1.typeof操作符總是返回一個(gè)字符串,該字符串的含義為獲取對(duì)象的類型。
typeof 123;//"number"
typeof NaN;//"number"
typeof 'yyc';//"string"
typeof '';//"string"
typeof true;//"boolean"
typeof null;//"object"
typeof undefined;//"undefined"
typeof [];//"object"
typeof {};//"object"
2.因?yàn)?code>null,Array普通對(duì)象使用typeof操作符的結(jié)果均為object,因此,無法對(duì)這三者進(jìn)行進(jìn)一步區(qū)分?

解決:MDZ : Object.prototype.toString()

1.toString()返回一個(gè)代表該對(duì)象的字符串。
//普通對(duì)象
var Person = {
    name: 'yyc',
    age: 21
};
Person.toString();
//"[object Object]"

var o = new Object();
o.toString();
//"[object Object]"

//數(shù)組
var arr = [1,2,3];
arr.toString();
"1,2,3"
2.可以對(duì)默認(rèn)的toString方法進(jìn)行改寫。
function Dog(name,breed,color,sex){
    this.name = name;
    this.breed = breed;
    this.color = color;
    this.sex = sex;
}
theDog = new Dog('Gabby','Lab','chocolate','female');
//lt returns the default value inherited from Object
//Because you call the toString() method on a custom object
theDog.toString();//"[object Object]"

Dog.prototype.toString = function dogToString(){
    var ret = 'Dog ' + this.name + ' is a ' + this.sex +
    ' ' + this.color + ' ' + this.breed;
    return ret;
};
theDog.toString();
//"Dog Gabby is a female chocolate Lab"
3.使用toString()可以檢測(cè)對(duì)象的類型,通過Object.ptototype.toString()call()apply()的組合,將你想要檢測(cè)的對(duì)象作為call()apply()的第一個(gè)參數(shù),即可檢測(cè)每一個(gè)對(duì)象的類型。
Object.prototype.toString.call({});
//"[object Object]"
Object.prototype.toString.call(new Array);
//"[object Array]"
Object.prototype.toString.call(new Date);
//"[object Date]"
Object.prototype.toString.call(new Object);
//"[object Object]"
Object.prototype.toString.call(new String);
//"[object String]"
Object.prototype.toString.call(Math);
//"[object Math]"

//Since JavaScript 1.8.5
Object.prototype.toString.call(null);
//"[object Null]"
Object.prototype.toString.call(undefined);
//"[object Undefined]


3.包裝對(duì)象----不推薦使用
  • 是什么?通過new將基本類型(Number,String,Boolean)轉(zhuǎn)化為對(duì)象。
//Number
var n = new Number(123);
n;
>>>
Number{
__proto__:Number
[[PrimitiveValue]]:123
}

Object.prototype.toString.call(n);
//"[object Number]"

//String
var s = new String('yyc');
s;
>>>
String{
0:"y"
1:"y"
2:"c"
length:3
__proto__:String
[[PrimitiveValue]]:"yyc"
}

Object.prototype.toString.call(s);
//"[object String]"

//Boolean
var b = new Boolean(false);
b;
>>>
Boolean {
__proto__:Boolean
[[PrimitiveValue]]:false
}

Object.prototype.toString.call(b);
//"[object Boolean]"
4.如果我們單獨(dú)使用Number,String,Boolean,前面沒有加new會(huì)發(fā)生什么?

①Number---將傳入的參數(shù)的數(shù)據(jù)類型轉(zhuǎn)化為number,如果轉(zhuǎn)換后,不是數(shù)值,則輸出NaN

Number(123);
>>>123

Number('123');
>>>123

Number('yyc');
>>>NaN

Number(true);
>>>1

Number(false);
>>>0

Number('false');
>>>NaN

Number(null);
>>>0

Number(undefined);
>>>NaN

Number([]);
>>>0

Number([1,2,3]);
>>>NaN

②String---將傳入的參數(shù)的數(shù)據(jù)類型轉(zhuǎn)化為String

String(1);
>>>"1"

String('yyc');
>>>"yyc"

String(true);
>>>"true"

String(false);
>>>"false"

String('false');
>>>"false"

String(null);
>>>"null"

String(undefined);
>>>"undefined"

String([]);
>>>""

String([1,2,3]);
>>>"1,2,3"

String({});
>>>"[object Object]"

③Boolean----空字符串 、 0 、 false 、 nullundefinedNaN ,結(jié)果為 false

Boolean(1);
>>>true

Boolean(0);
>>>false

Boolean('yyc');
>>>true

Boolean(false);
>>>false

//Be careful
Boolean('false');
>>>true

Boolean(null);
>>>false

Boolean(undefined);
>>>false

Boolean([]);
>>>true

Boolean({});
>>>true

Boolean('');
>>>false

Boolean(NaN);
>>>false

④number類型調(diào)用toString()

123.toString();
>>>Uncaught SyntaxError: Invalid or unexpected token

//注意兩個(gè)點(diǎn)
123..toString();
>>>"123"

(123).toString();
>>>"123"


二.Date

1.是什么?Date對(duì)象用來表示日期和時(shí)間。
2.一個(gè)設(shè)計(jì)缺陷:月份用整數(shù)[0,11]表示,0表示一月,1表示二月...
//september  ==   8
var now = new Date();
now;
>>>Fri Sep 01 2017 13:44:00 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)

now.getMonth();
>>>8
3.時(shí)間戳(time stamp)是什么?它是一個(gè)自增的整數(shù),它表示從1970年1月1日零時(shí)整的GMT(GreenWich Mean Time)時(shí)區(qū)開始的那一刻,到現(xiàn)在的毫秒數(shù)。
now.getTime();
>>>1504244640903

var d = new Date(1504244640903);
d.toLocaleString();
>>>"2017/9/1 下午1:44:00"

//UTC(Coordinated Universal Time世界協(xié)調(diào)時(shí)間)
d.toUTCString();
>>>"Fri, 01 Sep 2017 05:44:00 GMT"

//推算一下
1970+1504244640903/1000/60/60/24/365;
>>>2017.6992846557268
4.如何獲取當(dāng)前時(shí)間戳?
if(Date.now){
    console.log(Date.now());//兼容IE老版本
}else{
    console.log(new Date().getTime());
}

拓展:split()----分割

MDN : String.prototype.split()

The split() method splits a String object into an array of strings by separating the string into substrings,using a specified separator string to determine where to make each split.

Syntax:
str.split([separator[, limit]])

Description

①When found,separator is removed from the string,and the substrings are returned in an array.[當(dāng)在字符串中找到分隔符時(shí),刪除該分隔符,并將這段子字符串(①上一個(gè)分隔符到該分隔符之間的內(nèi)容 ②字符串起始處到該分隔符的內(nèi)容 ③空字符串,當(dāng)分隔符在字符串起始處)返回到一個(gè)數(shù)組中。]
②If separator is not found or is omitted,the array contains one element consisting of the entire string.
var s = 'yyc';
s.split('a');
>>>["yyc"]

s.split();
>>>["yyc"]
③If seperator is an empty string,str is converted to an array of characters.
var s = ' yyc';
s.split('');
>>>(4) [" ", "y", "y", "c"]
④If seperator appears at the beginning or end of the string,or both,the array begins,ends,or both begins and ends,repectively,with an empty string.
var s = 'yyc';
s.split('y');
>>>(3) ["", "", "c"]

var s = 'ycc';
s.split('y');
>>(2) ["", "cc"]

var s = 'cyc';
s.split('y');
>>>(2) ["c", "c"]

var s = 'yyyc';
s.split('y');
>>>(4) ["", "", "", "c"]

var s = 'yyc';
s.split('c');
>>>(2) ["yy", ""]

var s = 'yycc';
s.split('c');
>>>(3) ["yy", "", ""]

var s = 'cyyc';
s.split('c');
>>>(3) ["", "yy", ""]

var s = 'y';
s.split('y');
>>>(2) ["", ""]
split()split(' ')split('')
var a = 'Oh brave new world that has such people in it.';
a.split();
>>>["Oh brave new world that has such people in it."]

var a = 'Oh brave new world that has such people in it.';
a.split(' ');
>>>(10) ["Oh", "brave", "new", "world", "that", "has", "such", "people", "in", "it."]

var a = 'Oh brave new world that has such people in it.';
a.split('');
>>>(46) ["O", "h", " ", "b", "r", "a", "v", "e", " ", "n", "e", "w", " ", "w", "o", 
"r", "l", "d", " ", "t", "h", "a", "t", " ", "h", "a", "s", " ", "s", "u", "c", "h", 
" ", "p", "e", "o", "p", "l", "e", " ", "i", "n", " ", "i", "t", "."]
⑥Returning a limited numbers of splits
var  s = 'Hello world, how are you doing';
s.split(' ',3);
>>>(3) ["Hello", "world,", "how"]

s.split(' ',4);
>>>(4) ["Hello", "world,", "how", "are"]

圖解HTTP

第三章-HTTP報(bào)文內(nèi)的HTTP信息

HTTP通信過程包括從客戶端發(fā)往服務(wù)器端的請(qǐng)求,和從服務(wù)器端返回客戶端的響應(yīng)。

1.HTTP報(bào)文是什么?用于HTTP協(xié)議交互的信息。---報(bào)文結(jié)構(gòu)圖
報(bào)文結(jié)構(gòu).png
  • 通常,報(bào)文主體等于實(shí)體主體。只有當(dāng)傳輸中進(jìn)行編碼操作時(shí),實(shí)體主體的內(nèi)容發(fā)生變化,才導(dǎo)致它和報(bào)文主體產(chǎn)生差異。


2.編碼提升傳輸速率
編碼提升效率.png
3.發(fā)送多種數(shù)據(jù)的多部分對(duì)象集合
multipart.png

①存在的原因?在傳輸過程中可能會(huì)遇到不同類型的數(shù)據(jù),如:當(dāng)我們可以在郵件中輸入文字并添加附件。

②如何實(shí)現(xiàn)的?因?yàn)椴捎昧?strong>MIME(Multipurpose Internet Mail Extensions,多用途互聯(lián)網(wǎng)郵件擴(kuò)展)機(jī)制,它允許郵件處理文本、圖片、視頻等多個(gè)不同類型的數(shù)據(jù)。

③More:MIME擴(kuò)展中使用了一種稱為多部分對(duì)象集合(Multipart)的方法,來容納多分不同類型的數(shù)據(jù)。相應(yīng)地,HTTP協(xié)議也采納了多部分對(duì)象集合,發(fā)送的一份報(bào)文主體可包含多種類型實(shí)體。

④在HTTP報(bào)文中使用Multipart時(shí),需要在首部字段里加上Content-type

4.獲取部分內(nèi)容的范圍請(qǐng)求

①存在的原因?由于以前的帶寬較慢,下載一張尺寸稍大的圖片或文件就很吃力了。更頭疼的是,如果下載過程中遇到網(wǎng)絡(luò)中斷的情況,下載就必須從頭開始。因此需要一種可恢復(fù)機(jī)制,即從之前加載中斷處恢復(fù)下載,保持連續(xù)性。

②范圍請(qǐng)求是什么?要實(shí)現(xiàn)上述功能,就需要指定下載的實(shí)體范圍。像這樣,指定范圍發(fā)送的請(qǐng)求叫做范圍請(qǐng)求(Range Request)。

范圍請(qǐng)求.png
5.內(nèi)容協(xié)商返回最合適的內(nèi)容

①存在的原因?同一個(gè)Web網(wǎng)站可能存在多份內(nèi)容相同的頁面。比如:MDN上同一個(gè)頁面可選擇多種語言閱讀。

②內(nèi)容協(xié)商機(jī)制:客戶端和服務(wù)器端就響應(yīng)的資源內(nèi)容進(jìn)行交涉,然后提供給客戶端最為合適的資源。

③判斷"合適"的標(biāo)準(zhǔn)?如:語言、字符集、編碼方式等,大多都包含在請(qǐng)求報(bào)文的某些首部字段中。

內(nèi)容協(xié)商.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容