将模板字符串传递给不带括号的函数

时间:2018-11-14 17:31:20

标签: javascript v8

在所有graphql文献中,推荐的创建graphql类型的方式如下:

import {gql} from 'graphql-tag'
const query = const typeDefs = gql`

type Book {
  title: String
  author: String
}

# The "Query" type is the root of all GraphQL queries.

type Query {
  books: [Book]
}

`;

我以前从未见过将参数传递给没有括号的函数,因此我做了一些测试

const fn = (n) => n+1

// pass a number

fn1; // Reference error can't find fn1

// Pass a string

fn'1' // Syntax error Unexpected string literal '1'

// Pass a template

fn`1` // => "11"

引擎中发生了哪种黑魔法,可以通过模板字符串来实现?我猜想这与js引擎如何解释模板字符串有关,大概是将它们隐式地括在括号中,但是我不知道。

0 个答案:

没有答案