标准对象 | toString() | Date | split() | HTTP报文

一 标准对象

1.typeof操作符总是返回一个字符串,该字符串的含义为获取对象的类型。
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.因为null,Array普通对象使用typeof操作符的结果均为object,因此,无法对这三者进行进一步区分?

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

1.toString()返回一个代表该对象的字符串。
//普通对象
var Person = {
    name: 'yyc',
    age: 21
};
Person.toString();
//"[object Object]"

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

//数组
var arr = [1,2,3];
arr.toString();
"1,2,3"
2.可以对默认的toString方法进行改写。
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()可以检测对象的类型,通过Object.ptototype.toString()call()apply()的组合,将你想要检测的对象作为call()apply()的第一个参数,即可检测每一个对象的类型。
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.包装对象—-不推荐使用
  • 是什么?通过new将基本类型(Number,String,Boolean)转化为对象。
//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.如果我们单独使用Number,String,Boolean,前面没有加new会发生什么?

①Number—将传入的参数的数据类型转化为number,如果转换后,不是数值,则输出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—将传入的参数的数据类型转化为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—-空字符串0falsenullundefinedNaN ,结果为 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类型调用toString()

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

//注意两个点
123..toString();
>>>"123"

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

二.Date

1.是什么?Date对象用来表示日期和时间。
2.一个设计缺陷:月份用整数[0,11]表示,0表示一月,1表示二月…
//september  ==   8
var now = new Date();
now;
>>>Fri Sep 01 2017 13:44:00 GMT+0800 (中国标准时间)

now.getMonth();
>>>8
3.时间戳(time stamp)是什么?它是一个自增的整数,它表示从1970年1月1日零时整的GMT(GreenWich Mean Time)时区开始的那一刻,到现在的毫秒数。
now.getTime();
>>>1504244640903

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

//UTC(Coordinated Universal Time世界协调时间)
d.toUTCString();
>>>"Fri, 01 Sep 2017 05:44:00 GMT"

//推算一下
1970+1504244640903/1000/60/60/24/365;
>>>2017.6992846557268
4.如何获取当前时间戳?
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.[当在字符串中找到分隔符时,删除该分隔符,并将这段子字符串(①上一个分隔符到该分隔符之间的内容 ②字符串起始处到该分隔符的内容 ③空字符串,当分隔符在字符串起始处)返回到一个数组中。]
②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报文内的HTTP信息

HTTP通信过程包括从客户端发往服务器端的请求,和从服务器端返回客户端的响应。

1.HTTP报文是什么?用于HTTP协议交互的信息。—报文结构图

《标准对象 | toString() | Date | split() | HTTP报文》 报文结构.png

  • 通常,报文主体等于实体主体。只有当传输中进行编码操作时,实体主体的内容发生变化,才导致它和报文主体产生差异。
2.编码提升传输速率

《标准对象 | toString() | Date | split() | HTTP报文》 编码提升效率.png

3.发送多种数据的多部分对象集合

《标准对象 | toString() | Date | split() | HTTP报文》 multipart.png

①存在的原因?在传输过程中可能会遇到不同类型的数据,如:当我们可以在邮件中输入文字并添加附件。

②如何实现的?因为采用了MIME(Multipurpose Internet Mail Extensions,多用途互联网邮件扩展)机制,它允许邮件处理文本、图片、视频等多个不同类型的数据。

③More:MIME扩展中使用了一种称为多部分对象集合(Multipart)的方法,来容纳多分不同类型的数据。相应地,HTTP协议也采纳了多部分对象集合,发送的一份报文主体可包含多种类型实体。

④在HTTP报文中使用Multipart时,需要在首部字段里加上Content-type

4.获取部分内容的范围请求

①存在的原因?由于以前的带宽较慢,下载一张尺寸稍大的图片或文件就很吃力了。更头疼的是,如果下载过程中遇到网络中断的情况,下载就必须从头开始。因此需要一种可恢复机制,即从之前加载中断处恢复下载,保持连续性。

②范围请求是什么?要实现上述功能,就需要指定下载的实体范围。像这样,指定范围发送的请求叫做范围请求(Range Request)。

《标准对象 | toString() | Date | split() | HTTP报文》 范围请求.png

5.内容协商返回最合适的内容

①存在的原因?同一个Web网站可能存在多份内容相同的页面。比如:MDN上同一个页面可选择多种语言阅读。

②内容协商机制:客户端和服务器端就响应的资源内容进行交涉,然后提供给客户端最为合适的资源。

③判断”合适”的标准?如:语言、字符集、编码方式等,大多都包含在请求报文的某些首部字段中。

《标准对象 | toString() | Date | split() | HTTP报文》 内容协商.png

    原文作者:姚屹晨
    原文地址: https://www.jianshu.com/p/66fe8c2bc256
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞