具有类型和默认值的Javascript方法

时间:2020-04-19 12:05:57

标签: javascript function react-native

我看过以下形式的代码

template <typename T> struct BinaryTree
{
  Node<T>* root = nullptr;

  explicit BinaryTree(Node<T>* const root)
    : root{ root }, pre_order{ *this }
  {
    root->set_tree(this);
  }

  ~BinaryTree() { if (root) delete root; }

  template <typename U>
  struct PreOrderIterator
  {
    Node<U>* current;

    explicit PreOrderIterator(Node<U>* current)
      : current(current)
    {
    }

    bool operator!=(const PreOrderIterator<U>& other)
    {
      return current != other.current;
    }

    // no continuations in C++ (unlike C#)
    PreOrderIterator<U>& operator++() 
    {
      if (current->right)
      {
        current = current->right;
        while (current->left)
          current = current->left;
      }
      else
      {
        Node<T>* p = current->parent;
        while (p && current == p->right)
        {
          current = p;
          p = p->parent;
        }
        current = p;
      }
      return *this;
    }

    Node<U>& operator*() { return *current; }
  };

  typedef PreOrderIterator<T> iterator;

  iterator end()
  {
    return iterator{ nullptr };
  }

  iterator begin()
  {
    Node<T>* n = root;

    if (n)
      while (n->left)
        n = n->left;
    return iterator{ n };
  }

};

我是Java语言的新手,并且从3个月开始使用react-native。 所以我知道这是一种函数声明。

我想知道:

  1. 此类声明的文档在哪里?
  2. 是否以任何名称知道这种声明?

1 个答案:

答案 0 :(得分:1)

我认为您正在将TypeScript与JavaScript混淆。 您发布的代码段位于TypeScript中。 TypeScript是Microsoft开发的JavaScript的超集。

要了解有关函数如何在TypeScript中工作的更多信息,请参考此内容, https://www.typescriptlang.org/docs/handbook/functions.html

一般来说,要了解和学习TypeScript,建议您从文档开始, https://www.typescriptlang.org/docs/home

完成后,可以在Udemy,Pluralsight等在线平台上学习一些课程。