一、數(shù)組的拓展
1、Array.from();
用于將兩種情況下的對(duì)象轉(zhuǎn)化為數(shù)組:
(1)、類(lèi)似數(shù)組的對(duì)象
(2)、可遍歷的對(duì)象
參數(shù)
(1)、被轉(zhuǎn)換的的對(duì)象。
(2)、map函數(shù)。
(3)、函數(shù)中this指向的對(duì)象。
let obj = {
render: function(x){
return x;
}
}
function a(){
var _arg = Array.prototype.slice.call(arguments);
var _from = Array.from(_arg,function(v){return this.render();},obj);
console.log(_arg); //[1,2]
console.log(_from); //[1,2]
}
a(1,2);
類(lèi)數(shù)組中的length屬性比實(shí)際屬性多的情況下 會(huì)做截取
傳入數(shù)組的情況下會(huì)返回新的數(shù)組,值傳遞
會(huì)忽略空位
2、Array.of();
生成一個(gè)數(shù)組或?qū)⒁唤M值轉(zhuǎn)換為數(shù)組。
主要用于規(guī)范 Array() 或者 new Array();
區(qū)別
Array.of(1) //[1]
Array.of(2,2) //[2,2]
Array.of(NaN) //
Array(2,2) //[2,2]
Array(1) //[undefined]
3、Array.find()
用于找出第一個(gè)符合條件的數(shù)組元素,找不到則返回undefined
參數(shù)
(1)callback
(2)函數(shù)中this指向的對(duì)象
回調(diào)函數(shù)中接收的參數(shù)
(1)、value
(2)、index
(3)、當(dāng)前數(shù)組的引用
var a = [1,2,3,4,5];
a.find(function(value, index, arr) {
return value > 9;
})
4、Array.findIndex();
用于找出第一個(gè)符合條件的數(shù)組元素的下標(biāo),找不到則返回-1
和indexOf()的區(qū)別在于 對(duì)NaN 的識(shí)別
5、Array.fill();
用于填充一個(gè)數(shù)組
參數(shù)
(1)、要填充的元素
(2)、起始位置
(3)、結(jié)束位置 不包含
new Array(100).fill(10)
6、Array.keys() Array.values() Array.entries()
用于遍歷數(shù)組。
區(qū)別
(1)、keys()是對(duì)鍵名的遍歷
(2)、values()是對(duì)鍵值的遍歷
(3)、entries()是對(duì)鍵值對(duì)的遍歷
返回值
返回一個(gè)遍歷器,可以用for...of循環(huán)進(jìn)行遍歷
var a = ['a','b'].entries()
for( let item of a ){
console.log(item); //[0, "a"][1, "b"]
}
for...in和Object.keys()區(qū)別在于繼承
7、Array.copyWithin()
用于把這個(gè)數(shù)組的一部分元素復(fù)制到其他位置
參數(shù)
(1)開(kāi)始替換的位置
(2)copy的起始位置
(3)結(jié)束位置
let arr = [1, 2, 3, 4, 5, 6, 7, 8];
arr.copyWithin(1, 5, 8);
console.log(arr);
拓展運(yùn)算符...
let a = [1,2,3];
let b = [...a];
二、對(duì)象的拓展
幾個(gè)簡(jiǎn)寫(xiě)
var f = {
url,
data(x , y){
return {x , y};
}
}
//等同于
var f = {
url: url,
data: function(x , y){
return {x:x , y:y};
}
}
屬性名表達(dá)式
obj.a = 1;
obj['a' + 'asd'] = 2;
let prop = 'foo';
let obj = {
[prop]: true,
['a' + 'bc']: 123
};
定義方法名
let obj = {
['h' + 'ello']() {
return 'hi';
}
};
1、Object.is()
用來(lái)比較兩個(gè)值是否嚴(yán)格相等。它與嚴(yán)格比較運(yùn)算符(===)的行為基本一致
不同點(diǎn)
+0 === -0 //true
NaN === NaN // false
Object.is(+0, -0) // false
Object.is(NaN, NaN) // true
2、Object.assign()
用來(lái)將源對(duì)象的所有可枚舉屬性,復(fù)制到目標(biāo)對(duì)象
參數(shù)
(1)、目標(biāo)對(duì)象
(2)、源對(duì)象 可多個(gè)
同名屬性 會(huì)覆蓋 后面 覆蓋前面
var target = { a: 1, b: 1 };
var source1 = { b: 2, c: 2 };
var source2 = { c: 3 };
Object.assign(target, source1, source2);
Object.assign(undefined) // 報(bào)錯(cuò)
Object.assign(null) // 報(bào)錯(cuò)
const v1 = 'abc';
const v2 = true;
const v3 = 10;
const obj = Object.assign({}, v1, v2, v3);
console.log(obj);
引用拷貝
同時(shí)也可以用于數(shù)組
Object.assign([1, 2, 3], [4, 5])
3、屬性的遍歷
(1)for...in
for...in循環(huán)遍歷對(duì)象自身的和繼承的可枚舉屬性(不含 Symbol 屬性)。
(2)Object.keys(obj)
Object.keys返回一個(gè)數(shù)組,包括對(duì)象自身的(不含繼承的)所有可枚舉屬性(不含 Symbol 屬性)的鍵名。
(3)Object.getOwnPropertyNames(obj)
Object.getOwnPropertyNames返回一個(gè)數(shù)組,包含對(duì)象自身的所有屬性(不含 Symbol 屬性,但是包括不可枚舉屬性)的鍵名。
(4)Object.getOwnPropertySymbols(obj)
Object.getOwnPropertySymbols返回一個(gè)數(shù)組,包含對(duì)象自身的所有 Symbol 屬性的鍵名。
(5)Reflect.ownKeys(obj)
Reflect.ownKeys返回一個(gè)數(shù)組,包含對(duì)象自身的所有鍵名,不管鍵名是 Symbol 或字符串,也不管是否可枚舉。
4、Object.setPrototypeOf() Object.getPrototypeOf()
Object.setPrototypeOf()
用于設(shè)置一個(gè)對(duì)象的__proto__
參數(shù)
(1)、Object: 設(shè)置的對(duì)象
(2)、proto: proto值
如果第一個(gè)參數(shù)不是對(duì)象,會(huì)自動(dòng)轉(zhuǎn)為對(duì)象。但是由于返回的還是第一個(gè)參數(shù),所以這個(gè)操作不會(huì)產(chǎn)生任何效果。
Object.getPrototypeOf()
用于獲取
三、函數(shù)的拓展
1、rest 參數(shù) 表現(xiàn)形式為 ...values
function a(...values){
return values.sort();
}
a(1,2,3);
后面不允許跟參數(shù)
2、箭頭函數(shù)
例如: const x = y => y;
function x(y){
return y;
}
x(){}
如果不需要參數(shù) 或者需要多個(gè)參數(shù) 就將參數(shù)部分用一個(gè)括號(hào)括起來(lái)
const x = () => 5;
const x = (v1,v2) => v1 + v2;
如果函數(shù)內(nèi)部執(zhí)行語(yǔ)句多于一條時(shí),需要用大括號(hào)括起來(lái)
const x = (v1,v2) => { return v1 + v2; }
原因在于代碼塊
函數(shù)體內(nèi)的this對(duì)象,就是定義時(shí)所在的對(duì)象,而不是使用時(shí)所在的對(duì)象。
不可以當(dāng)作構(gòu)造函數(shù),也就是說(shuō),不可以使用new命令,否則會(huì)拋出一個(gè)錯(cuò)誤
不可以使用arguments對(duì)象,該對(duì)象在函數(shù)體內(nèi)不存在。如果要用,可以用 rest 參數(shù)代替
this指向原因
3、尾調(diào)用優(yōu)化
就是指某個(gè)函數(shù)的最后一步是調(diào)用另一個(gè)函數(shù)。
以下三種情況,都不屬于尾調(diào)用。
// 情況一
function f(x){
let y = g(x);
return y;
}
// 情況二
function f(x){
return g(x) + 1;
}
// 情況三
function f(x){
g(x);
}
特別針對(duì) 尾調(diào)用 調(diào)用遞歸
ES2017 允許函數(shù)的最后一個(gè)參數(shù)有尾逗號(hào)(trailing comma)。
四、class基本語(yǔ)法
function point(x,y){
this.x = x;
this.y = y;
}
point.prototype.toString = function(){
return '(' + this.x + ', ' + this.y + ')';
}
改寫(xiě)
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
toString() {
return '(' + this.x + ', ' + this.y + ')';
}
}
方法之間不需要寫(xiě)逗號(hào)
ES6 的類(lèi),完全可以看作構(gòu)造函數(shù)的另一種寫(xiě)法。
class Point {
}
typeof Point // "function"
Point === Point.prototype.constructor // true
在類(lèi)的實(shí)例上面調(diào)用方法,其實(shí)就是調(diào)用原型上的方法。
class Point {
constructor(){
// ...
}
}
Object.assign(Point.prototype, {
toString(){},
toValue(){}
});
class里面定義的方法 都是不可枚舉的
Object.keys(class)
Object.getOwnPropertyNames(class)
es5中的 是可以枚舉的
生成類(lèi)的實(shí)力方法和es5一樣 都是通過(guò)new的方法來(lái)實(shí)現(xiàn)的 但是并不存在變量提升 必須在class下 去new
ES5 一樣,實(shí)例的屬性除非顯式定義在其本身(即定義在this對(duì)象上),否則都是定義在原型上(即定義在class上)。
//定義類(lèi)
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
toString() {
return '(' + this.x + ', ' + this.y + ')';
}
}
var point = new Point(2, 3);
point.toString() // (2, 3)
point.hasOwnProperty('x') // true
point.hasOwnProperty('y') // true
point.hasOwnProperty('toString') // false
point.__proto__.hasOwnProperty('toString') // true
類(lèi)也可以使用表達(dá)式的形式定義。
const MyClass = class Me {
getClassName() {
return Me.name;
}
};
類(lèi)名為MyClass 而非 Me
let inst = new MyClass();
inst.getClassName()
Me.name
也可以寫(xiě)成立即執(zhí)行函數(shù)
也可以使用get和set 去設(shè)定某個(gè)屬性的get 和set
class MyClass {
constructor() {
// ...
}
get prop() {
return 'getter';
}
set prop(value) {
console.log('setter: '+value);
}
}
let inst = new MyClass();
inst.prop = 123; // set 123
inst.prop
Class 的靜態(tài)方法
只需要在前面加 static 關(guān)鍵字
靜態(tài)方法不會(huì)被new出來(lái)的實(shí)例繼承,只能通過(guò)類(lèi)來(lái)調(diào)用
比如
class foo{
static aaa(){
return 'hello';
}
}
new foo().aaa() // no