-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/yihan12/Blog
- Loading branch information
Showing
12 changed files
with
799 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Array | ||
|
||
# Type Array |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# 数据结构 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
# 对象 | ||
|
||
> 在JavaScript中,对象为王。如果你理解对象,你就理解了JavaScript。 | ||
在JavaScript中,几乎“一切”都是对象。 | ||
- Booleans | ||
- Numbers | ||
- Strings | ||
- Dates | ||
- Maths | ||
- RegExp | ||
- Arrays | ||
- Functions | ||
- Objects | ||
|
||
### 创建对象四种方法 | ||
- 使用构造函数 | ||
- 使用对象字面量 | ||
- 使用Object.create()方法创建对象 | ||
- 使用es6 Class | ||
|
||
#### 使用构造函数 | ||
```javascript | ||
function vehicle(name,maker,engine){ | ||
this.name = name; | ||
this.maker = maker; | ||
this.engine = engine; | ||
} | ||
|
||
let car = new vehicle('GT','BMW','1998cc'); | ||
// OUTPUT: vehicle {name: 'GT', maker: 'BMW', engine: '1998cc'} | ||
// GT | ||
// BMW | ||
// 1998cc | ||
console.log(car); | ||
console.log(car.name); | ||
console.log(car.maker); | ||
console.log(car['engine']); | ||
``` | ||
|
||
```javascript | ||
const o = new Object(); | ||
o.foo = 42; | ||
|
||
console.log(o); | ||
// { foo: 42 } | ||
``` | ||
|
||
#### 使用对象字面量 | ||
```javascript | ||
let car = { | ||
name : 'GT', | ||
maker : 'BMW', | ||
engine : '1998cc' | ||
}; | ||
console.log(car.name); //dot notation | ||
console.log(car['maker']); //bracket notation | ||
``` | ||
|
||
#### 使用Object.create() | ||
```javascript | ||
const coder = { | ||
isStudying : false, | ||
printIntroduction : function(){ | ||
console.log(`My name is ${this.name}. Am I studying?: ${this.isStudying}`); | ||
} | ||
}; | ||
const me = Object.create(coder); | ||
me.name = 'Mukul'; | ||
me.isStudying = true; | ||
me.printIntroduction(); // My name is Mukul. Am I studying?: true | ||
``` | ||
|
||
#### 使用ES6 Class | ||
```javascript | ||
class Vehicle { | ||
constructor(name, maker, engine) { | ||
this.name = name; | ||
this.maker = maker; | ||
this.engine = engine; | ||
} | ||
} | ||
|
||
let car1 = new Vehicle('GT', 'BMW', '1998cc'); | ||
|
||
console.log(car1.name); // 'GT' | ||
car1.name = '123' | ||
console.log(car1); // Vehicle {name: '123', maker: 'BMW', engine: '1998cc'} | ||
console.log(car1.name); // '123' | ||
``` | ||
|
||
### 对象删除 | ||
对象只能删除本身属性,不能删除继承的属性。 | ||
|
||
例1: | ||
```javascript | ||
let obj1 = { | ||
propfirst : "Name" | ||
} | ||
|
||
// Output : Name | ||
console.log(obj1.propfirst); | ||
delete obj1.propfirst | ||
|
||
// Output : undefined | ||
console.log(obj1.propfirst); | ||
``` | ||
|
||
例2: | ||
```javascript | ||
let obj1 = { | ||
propfirst : "Name" | ||
} | ||
// Output : Name | ||
console.log(obj1.propfirst) | ||
let obj2 = Object.create(obj1); | ||
|
||
// Output : Name | ||
console.log(obj2.propfirst); | ||
|
||
// Output : true. | ||
console.log(delete obj2.propfirst); | ||
|
||
// Output : Name | ||
console.log(obj2.propfirst); | ||
|
||
// Output : true | ||
console.log(delete obj1.propfirst); | ||
|
||
// Output : undefined | ||
console.log(obj2.propfirst); | ||
``` | ||
对比例1和例2不难发现,delete能删除本身的属性,但是不能删除继承的属性。删除调obj1的propfirst属性后,会影响到obj2的继承属性。 | ||
|
||
### 遍历对象 | ||
|
||
- for...in 循环 该方法依次访问一个对象及其原型链中所有可枚举的属性。 | ||
- Object.keys(o) 该方法返回对象 o 自身包含(不包括原型中)的所有可枚举属性的名称的数组。 | ||
- Object.getOwnPropertyNames(o) 该方法返回对象 o 自身包含(不包括原型中)的所有属性 (无论是否可枚举) 的名称的数组。 | ||
- Reflect.ownKeys(target) 方法返回一个由目标对象自身的属性键组成的数组。它的返回值等同于 Object.getOwnPropertyNames(target).concat(Object.getOwnPropertySymbols(target))。 | ||
|
||
遍历对象的所有键:要遍历对象的所有现有可枚举键,我们可以在构造中使用for…in。值得注意的是,这允许我们只访问对象的可枚举属性(回想一下,可枚举是数据属性的四个属性之一)。例如,从Object.prototype继承的属性是不可枚举的。但是,从某处继承的可枚举属性也可以使用for… in构造来访问 | ||
|
||
```javascript | ||
let person = { | ||
gender : "male" | ||
} | ||
|
||
let person1 = Object.create(person, | ||
{ | ||
getFooENUM: { | ||
value() { | ||
return this.foo; | ||
}, | ||
enumerable: true, | ||
}, | ||
getFoo: { | ||
value() { | ||
return this.foo; | ||
}, | ||
enumerable: false, | ||
}, | ||
},); | ||
person1.name = "Adam"; | ||
person1.age = 45; | ||
person1.nationality = "Australian"; | ||
|
||
|
||
for (let key in person1) { | ||
// Output : name, age, nationality, gender, getFooENUM | ||
console.log(key); | ||
} | ||
|
||
console.log(Object.keys(person1)) // ['getFooENUM', 'name', 'age', 'nationality'] | ||
console.log(Object.getOwnPropertyNames(person1)) // ['getFooENUM', 'getFoo', 'name', 'age', 'nationality'] | ||
console.log(Object.getOwnPropertySymbols(person1)) // [] | ||
console.log(Reflect.ownKeys(person1)) // ['getFooENUM', 'getFoo', 'name', 'age', 'nationality'] | ||
``` | ||
|
||
### 对象的setter和getter | ||
> getter 是一个获取某个特定属性的值的方法。 | ||
> setter 是一个设定某个属性的值的方法。 | ||
> 你可以为预定义的或用户定义的对象定义 getter 和 setter 以支持新增的属性。 | ||
```javascript | ||
const person = { | ||
firstName: "John", | ||
lastName: "Doe", | ||
fullName: function() { | ||
return this.firstName + " " + this.lastName; | ||
} | ||
}; | ||
|
||
console.log(person.fullName()) // 'John Doe' | ||
const person2 = { | ||
firstName: "John", | ||
lastName: "Doe", | ||
get fullName() { | ||
return this.firstName + " " + this.lastName; | ||
} | ||
}; | ||
|
||
console.log(person2.fullName) // 'John Doe' | ||
``` | ||
|
||
```javascript | ||
const person = { | ||
firstName: "John", | ||
lastName: "Doe", | ||
language: "", | ||
set lang(lang) { | ||
this.language = lang; | ||
} | ||
}; | ||
|
||
person.lang = "en"; | ||
|
||
console.log(person.language) // 'en' | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# 原型继承 | ||
|
||
> 原型继承是javascript中用于在对象中添加方法和属性的功能。它是一个对象可以继承另一个对象的属性和方法的方法。传统上,为了获取和设置对象的原型,我们使用Object.getPrototypeOf 和 Object.setPrototypeOf. | ||
当谈到继承时,JavaScript 只有一种结构:对象。每个对象(object)都有一个私有属性指向另一个名为原型(prototype)的对象。原型对象也有一个自己的原型,层层向上直到一个对象的原型为 null。根据定义,null 没有原型,并作为这个原型链(prototype chain)中的最后一个环节。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# 七种原始类型 | ||
- string | ||
- number | ||
- null | ||
- undefined | ||
- boolean | ||
- Symbol | ||
- BigInt | ||
|
||
# 对象数据类型 | ||
|
||
- object | ||
- array | ||
- date | ||
- ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
# js的相等性判断 | ||
JavaScript 提供三种不同的值比较运算: | ||
|
||
- ===——严格相等(三个等号) | ||
- ==——宽松相等(两个等号) | ||
- Object.is() | ||
|
||
对应的四个相等算法: | ||
|
||
- IsLooselyEqual:== | ||
- IsStrictlyEqual:=== | ||
- SameValue:Object.is() | ||
- SameValueZero:被许多内置运算使用 | ||
|
||
|
||
# 双等号(==) | ||
|
||
```javascript | ||
const num = 0; | ||
const big = 0n; | ||
const str = "0"; | ||
const obj = new String("0"); | ||
|
||
console.log(num == str); // true | ||
console.log(big == num); // true | ||
console.log(str == big); // true | ||
|
||
console.log(num == obj); // true | ||
console.log(big == obj); // true | ||
console.log(str == obj); // true | ||
|
||
// 对比NaN,+0,-0,{} | ||
console.log('NaN' , NaN == NaN) // NaN false | ||
console.log('+0,-0' , +0 == -0) // +0,-0 true | ||
let a = {},b={},c=d={} | ||
console.log('a,b' , a == b) // {} false | ||
console.log('c,d' , c == d) // {} true | ||
``` | ||
|
||
- 双等号(==)处理不同类型相同值时,返回true,例如(0,'0',0n,new String("0")); | ||
- 双等号(==)认为 +0等于-0 | ||
- 双等号(==)认为 NaN不等于NaN | ||
- 双等号(==)认为{}不等于{} | ||
|
||
# 三等号(===) | ||
|
||
```javascript | ||
const num = 0; | ||
const big = 0n; | ||
const str = "0"; | ||
const obj = new String("0"); | ||
|
||
console.log(num === str); // false | ||
console.log(big === num); // false | ||
console.log(str === big); // false | ||
|
||
console.log(num === obj); // false | ||
console.log(big === obj); // false | ||
console.log(str === obj); // false | ||
|
||
// 对比NaN,+0,-0,{} | ||
console.log('NaN' , NaN === NaN) // NaN false | ||
console.log('+0,-0' , +0 === -0) // +0,-0 true | ||
let a = {},b={},c=d={} | ||
console.log('a,b' , a === b) // {} false | ||
console.log('c,d' , c === d) // {} true | ||
``` | ||
|
||
- 三等号(===)处理不同类型相同值时,返回false,例如(0,'0',0n,new String("0")); | ||
- 三等号(===)认为 +0等于-0 | ||
- 三等号(===)认为 NaN不等于NaN | ||
- 三等号(===)认为{}不等于{} | ||
|
||
# Object.is() | ||
|
||
```javascript | ||
const num = 0; | ||
const big = 0n; | ||
const str = "0"; | ||
const obj = new String("0"); | ||
|
||
console.log(Object.is(num , str)); // false | ||
console.log(Object.is(big , num)); // false | ||
console.log(Object.is(str, big)); // false | ||
|
||
console.log(Object.is(num , obj)); // false | ||
console.log(Object.is(big , obj)); // false | ||
console.log(Object.is(str , obj)); // false | ||
|
||
// 对比NaN,+0,-0,{} | ||
console.log('NaN' , Object.is(NaN , NaN)) // NaN true | ||
console.log('+0,-0' , Object.is(+0 , -0)) // +0,-0 false | ||
let a = {},b={},c=d={} | ||
console.log('a,b' , Object.is(a , b)) // {} false | ||
console.log('c,d' , Object.is(c ,d)) // {} true | ||
``` | ||
|
||
- Object.is()处理不同类型相同值时,返回false,例如(0,'0',0n,new String("0")); | ||
- Object.is()认为 +0不等于-0 | ||
- Object.is()认为 NaN等于NaN | ||
- Object.is()认为{}不等于{} | ||
|
||
# sameValueZero | ||
|
||
```javascript | ||
function sameValueZero(x, y) { | ||
if (typeof x === "number" && typeof y === "number") { | ||
// x 和 y 相等(可能是 -0 和 0)或它们都是 NaN | ||
return x === y || (x !== x && y !== y); | ||
} | ||
return x === y; | ||
} | ||
const num = 0; | ||
const big = 0n; | ||
const str = "0"; | ||
const obj = new String("0"); | ||
|
||
console.log(sameValueZero(num , str)); // false | ||
console.log(sameValueZero(big , num)); // false | ||
console.log(sameValueZero(str, big)); // false | ||
|
||
console.log(sameValueZero(num , obj)); // false | ||
console.log(sameValueZero(big , obj)); // false | ||
console.log(sameValueZero(str , obj)); // false | ||
|
||
// 对比NaN,+0,-0,{} | ||
console.log('NaN' , sameValueZero(NaN , NaN)) // NaN true | ||
console.log('+0,-0' , sameValueZero(+0 , -0)) // +0,-0 true | ||
let a = {},b={},c=d={} | ||
console.log('a,b' , sameValueZero(a , b)) // {} false | ||
console.log('c,d' , sameValueZero(c ,d)) // {} true | ||
``` | ||
|
||
- sameValueZero处理不同类型相同值时,返回false,例如(0,'0',0n,new String("0")); | ||
- sameValueZero认为 +0等于-0 | ||
- sameValueZero认为 NaN等于NaN | ||
- sameValueZero认为 {}不等于{} |
Oops, something went wrong.