我被这里的语法所困扰:超级和递归方式的作用。
在下面的代码中,super.format是在名为"的函数中编写的;格式";。当我搜索super的定义时,它是父类,我猜这里是LinkBot。这个LinkBot类有一个叫做d格式的函数。所以,在我看来,这是以递归的方式制作的。
而且super.formats((是在formats((内部定义的,这对我来说真的很尴尬
有人能帮我这是什么吗。。?
期待着找到从丛林中拯救的人。。
import Parchment from 'parchment';
class LinkBlot extends Parchment.Inline {
static create(url) {
let node = super.create();
return node;
}
static formats(domNode) {
return domNode.getAttribute('href') || true;
}
format(name, value) {
if (name === 'link' && value) {
this.domNode.setAttribute('href', value);
} else {
super.format(name, value);
}
}
formats() {
let formats = super.formats();
formats['link'] = LinkBlot.formats(this.domNode);
return formats;
}
}
Parchment.register(LinkBlot);
它更多地与类的工作方式有关。我只是以你们班为例,以免压倒你们。
// 👇 Child class 👇 Parent Class
class LinkBlot extends Parchment.Inline {
}
是的,super口头上指代父类,意思是Parchment.Inline.
父类-Parchment.Inline
具有create()
、format()
、formats()
方法
class Parchment.Inline {
create() { ... }
format(name, value) { ... }
formats() { ... }
...
}
子类LinkBlot
通过使用extends
继承Parchment.Inline(父类(功能
举个例子,你可能会从你的母亲/父亲那里继承一些技能/行为,比如:
- 每周整理房子
同时,你可以进化并超越他们的技能/行为,拥有自己的版本
- 如果我有精力,请使用我自己的版本
- 每天整理房子
- 但如果我累了,就用父母的方式
- 每周整理房子
LinkBlot也在做同样的事情,它正在覆盖父format()
以拥有自己的format()
版本。
- if(name==='link'&&value(,用我自己的方式
- this.domNode.setAttribute('ref',value(
- 如果没有,请使用父版本
- super.format(名称、值(
class LinkBlot extends Parchment.Inline {
...
// overriding parent format() and create my own version
format(name, value) {
// if the following conditions meet, use my logic to set domNode attribut.
if (name === 'link' && value) {
this.domNode.setAttribute('href', value);
} else {
// Otherwise, use the parent version
super.format(name, value);
}
}
}
同样的原理也适用于formats()
和create()
。