Skip to content

Commit

Permalink
20230713
Browse files Browse the repository at this point in the history
  • Loading branch information
huxiguo committed Jul 13, 2023
1 parent b2ffcdb commit f7845f3
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .github/workflow/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
uses: actions/setup-node@v3
with:
# 选择要使用的 node 版本
node-version: '14.18.x'
node-version: "14.18.x"

# 缓存 node_modules
- name: Cache dependencies
Expand Down Expand Up @@ -49,7 +49,7 @@ jobs:
uses: crazy-max/ghaction-github-pages@v2
with:
# 部署到 gh-pages 分支
target_branch: master
target_branch: main
# 部署目录为 VuePress 的默认输出目录
build_dir: docs/.vuepress/dist
env:
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: docs

on:
# 每当 push 到 master 分支时触发部署
# 每当 push 到 main 分支时触发部署
push:
branches: [master]
branches: [main]
# 手动触发部署
workflow_dispatch:

Expand All @@ -21,7 +21,7 @@ jobs:
uses: actions/setup-node@v3
with:
# 选择要使用的 node 版本
node-version: '14.20.x'
node-version: "14.20.x"

# 缓存 node_modules
- name: Cache dependencies
Expand Down Expand Up @@ -49,7 +49,7 @@ jobs:
uses: crazy-max/ghaction-github-pages@v2
with:
# 部署到 gh-pages 分支
target_branch: master
target_branch: main
# 部署目录为 VuePress 的默认输出目录
build_dir: docs/.vuepress/dist
env:
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ node_modules

.cache

dist
/*/dist
2 changes: 1 addition & 1 deletion docs/.vuepress/dist
Submodule dist updated from a63ca0 to eccb3e
66 changes: 64 additions & 2 deletions docs/js/inheritance.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ description: javascript的继承

在子类别继承父类别的同时,可以重新定义某些属性,并重写某些方法,即覆盖父类别的原有属性和方法,使其获得与父类别不同的功能

虽然 JavaScript 并不是真正的面向对象语言,但它天生的灵活性,使应用场景更加
虽然 JavaScript 并不是真正的面向对象语言,但它天生的灵活性,使应用场景更加丰富

```js
// 动物类
Expand Down Expand Up @@ -192,8 +192,70 @@ p2.list.push(5); // [1,2,3,4,5]

`Object.create` 方法实现的是浅拷贝,多个实例的引用类型属性指向相同的内存,存在篡改的可能

原型式继承非常适合不需要单独创建构造函数,但仍然需要在对象间共享信息的场
原型式继承非常适合不需要单独创建构造函数,但仍然需要在对象间共享信息的场景

### 寄生式继承

```js
// 寄生式继承

let person = {
name: "aaaa",
getName: function () {
return this.name;
},
};

function parasitic(original) {
let res = Object.create(original);
res.sayHi = function () {
console.log(`hi${this.name}`);
};
return res;
}

let p1 = parasitic(person);

p1.name = "xxxx";
p1.sayHi();
```

可以为子类添加属性和方法,且不会对父类造成影响,但是对引用类型属性存在篡改的可能

### 寄生组合式继承

```js
// 寄生组合式继承

function Person(name) {
this.name = name;
this.sayHi = function () {
console.log("hi");
};
}

Person.prototype.sayHello = function () {
console.log(`hello${this.name}`);
};

function clone(parent, child) {
child.prototype = Object.create(parent.prototype);
child.prototype.constructor = child;
}

function Child(name) {
Person.call(this, name);
}

clone(Person, Child);

Child.prototype.getName = function () {
return this.name;
};

let c1 = new Child("xxxx");

c1.sayHi();
c1.sayHello();
console.log(c1.getName());
```

0 comments on commit f7845f3

Please sign in to comment.