首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >羽毛笔中的super.format是什么?

羽毛笔中的super.format是什么?
EN

Stack Overflow用户
提问于 2021-05-22 11:44:32
回答 1查看 96关注 0票数 0

我对这里的语法很感兴趣:超级和递归方式的角色。

在下面的代码中,super.format是在名为"format“的函数中编写的。当我搜索超级的定义时,它是父类,在这里我猜是LinkBot。这个LinkBot类有一个函数calle格式。所以,在我看来,这是以递归的方式制作的。

super.formats()也是在格式()中定义的,这在我看来确实很尴尬。

有人能帮我这是什么吗..?

期待着找到从这个丛林中拯救的人..。

代码语言:javascript
复制
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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-23 17:52:00

更多的是关于课堂如何工作的。我只是以你的课为例,以避免压倒你。

代码语言:javascript
复制
//     Child class    Parent Class
class LinkBlot extends Parchment.Inline {
}

是的,超级指的是父类,意思是Parchment.Inline.

父类- Parchment.Inlinecreate()format()formats()方法

代码语言:javascript
复制
class Parchment.Inline {
   create() { ... }
   format(name, value) { ... }
   formats() { ... }
   ...
}

子类LinkBlot通过使用extends继承Parchment.Inline (父类)功能

举个例子,你可以继承父母的一些技能/行为,比如:

  • 每周打扫房子。--

同时,您可能会进化并超越他们的技能/行为,以获得您自己的版本。

如果我有精力的话,用我自己的版本day

  • 整理房子

但是如果我累了,用父母的方式做week.

  • 整理房子

LinkBlot也是这样做的,它正在重写父format()以拥有自己的format()版本。

  • this.domNode.setAttribute('href',value);

  • if (命名为=== 'link‘& value),以我自己的方式进行

如果没有,请使用父版本

  • super.format(name,value);

代码语言:javascript
复制
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()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67649080

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档