我对这里的语法很感兴趣:超级和递归方式的角色。
在下面的代码中,super.format是在名为"format“的函数中编写的。当我搜索超级的定义时,它是父类,在这里我猜是LinkBot。这个LinkBot类有一个函数calle格式。所以,在我看来,这是以递归的方式制作的。
super.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);发布于 2021-05-23 17:52:00
更多的是关于课堂如何工作的。我只是以你的课为例,以避免压倒你。
// Child class Parent Class
class LinkBlot extends Parchment.Inline {
}是的,超级指的是父类,意思是Parchment.Inline.
父类- Parchment.Inline有create()、format()、formats()方法
class Parchment.Inline {
create() { ... }
format(name, value) { ... }
formats() { ... }
...
}子类LinkBlot通过使用extends继承Parchment.Inline (父类)功能
举个例子,你可以继承父母的一些技能/行为,比如:
同时,您可能会进化并超越他们的技能/行为,以获得您自己的版本。
如果我有精力的话,用我自己的版本day
但是如果我累了,用父母的方式做week.
LinkBlot也是这样做的,它正在重写父format()以拥有自己的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()。
https://stackoverflow.com/questions/67649080
复制相似问题