简体   繁体   English

以下来自 slate.js 示例的代码片段在做什么?

[英]What is the following code snippet from a slate.js example doing?

I am trying to understand Slate.js by looking through the rich text example , and I came across the following snippet of code, which I do not understand.我试图通过查看富文本示例来理解 Slate.js,但我遇到了以下我不理解的代码片段。

const isBlockActive = (editor, format) => {
    const [match] = Editor.nodes(editor, {
      match: n => n.type === format,
  })

  return !!match
}

I am not an expert in javascript, and I am new to both typescript and slate.js so I apologize in advance for not being able to frame my question better, but here is what I currently understand and what I am still unsure about:我不是 javascript 专家,而且我是 typescript 和 slate.js 的新手,所以我提前为无法更好地解决我的问题而道歉,但这是我目前的理解以及我仍然不确定的内容:

(1) Editor.nodes() is a method which returns an Iterable. (1) Editor.nodes() 是一种返回 Iterable 的方法。 What is the "const [match]" notation?什么是“const [match]”符号? Is this javascript or typescript?这是 javascript 还是打字稿?

(2) Is the " match " in " const [match] " the same as the " match " in " match : n => n.type === format "? (2)是“ match在”“ const [match] ”一样的“ match在”“ match : n => n.type === format ”? If so, does that mean " const [match] " is an array with one element which is a function?如果是这样,这是否意味着“ const [match] ”是一个数组,其中一个元素是一个函数? It seems odd if that were the case as then why bother making Editor.nodes() return an Iterable at all?如果是这样的话,这似乎很奇怪,那为什么还要让 Editor.nodes() 返回一个 Iterable 呢?

(3) I know double exclamation points give me a Boolean object, but since I can't wrap my head around whether match is a function or an iterable or something else, I have no idea what the truth or falsity of !!match tells me about the initial iterable returned by Editor.nodes(). (3) 我知道双感叹号给了我一个布尔对象,但由于我无法理解 match 是函数还是迭代或其他东西,我不知道!!match的真假我关于 Editor.nodes() 返回的初始迭代。

Thanks for any light you may be able to shed on my confused brain!感谢您对我困惑的大脑的任何启发!

That's called array destructuring .这就是所谓的数组解构 match is a variable that contains the first element of the array (or rather the first value yielded by the iterator) returned by the Editor.nodes function. match是一个变量,它包含Editor.nodes函数返回的数组的第一个元素(或者更确切地说是迭代器产生的第一个值)。 It barely equals:它几乎等于:

  const match = Editor.nodes(...)[0];

Or more accurately:或者更准确地说:

 const _it = Editor.nodes(...)[Symbol.iterator]().next();
 const match = _it.done ? undefined : _it.value;

(1) Editor.nodes() is a method which returns an Iterable. (1) Editor.nodes() 是一种返回 Iterable 的方法。 What is the "const [match]" notation?什么是“const [match]”符号? Is this javascript or typescript?这是 javascript 还是打字稿?

It's JavaScript (and TypeScript) array destructuring .它是 JavaScript(和 TypeScript)数组解构 Editor.nodes returns an iterable which is iterated over to create an array, and the first element of this array is assigned to match . Editor.nodes 返回一个可迭代对象,它被迭代以创建一个数组,并将该数组的第一个元素分配给match We're only interested in in whether there is at least one match, so looking at the first element of the array will tell us that.我们只对是否至少有一个匹配感兴趣,所以查看数组的第一个元素会告诉我们这一点。

(2) Is the "match" in "const [match]" the same as the "match" in "match : n => n.type === format"? (2) “const [match]”中的“match”与“match : n => n.type === format”中的“match”是否相同? If so, does that mean "const [match]" is an array with one element which is a function?如果是这样,这是否意味着“const [match]”是一个包含一个函数元素的数组? It seems odd if that were the case as then why bother making Editor.nodes() return an Iterable at all?如果是这样的话,这似乎很奇怪,那为什么还要让 Editor.nodes() 返回一个 Iterable 呢?

The two variables are completely different , and could have (should have?) been named differently to make things more clear.这两个变量完全不同,并且可以(应该?)以不同的方式命名以使事情更清楚。 Editor.nodes() is part of the Editor interface/API, which is used for many different things. Editor.nodes() 是Editor接口/API 的一部分,用于许多不同的事情。 In this case we're only interested in the first element.在这种情况下,我们只对第一个元素感兴趣。 You could use it to find and iterate over all nodes in editor.children which your match function returns true for.您可以使用它来查找和迭代 editor.children 中match函数为其返回 true 的所有节点。

(3) I know double exclamation points give me a Boolean object, but since I can't wrap my head around whether match is a function or an iterable or something else, I have no idea what the truth or falsity of !!match tells me about the initial iterable returned by Editor.nodes(). (3) 我知道双感叹号给了我一个布尔对象,但是因为我无法理解 match 是函数还是迭代或其他东西,所以我不知道 !!match 的真假我关于 Editor.nodes() 返回的初始迭代。

The resulting match is a node object , or undefined if it didn't match any.结果match是一个node object ,或者undefined如果它不匹配任何。 An object is truthy while undefined is falsey, and doing !!一个对象是真的,而 undefined 是假的,做 !! just converts it to a boolean.只是将其转换为布尔值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM