简体   繁体   English

打字稿中构造函数参数之前的装饰器语法是什么?

[英]What's this decorator syntax before constructor argument for in typescript?

What's the differences between constructor( @IStorageService storageService: IStorageService) and constructor( storageService: IStorageService) ? constructor( @IStorageService storageService: IStorageService)constructor( storageService: IStorageService)函数(storageService: IStorageService)有什么区别?

code from vscode first commit on github来自vscode 的代码首次提交到 github

import {TPromise} from 'vs/base/common/winjs.base';
import {Remotable, IThreadService} from 'vs/platform/thread/common/thread';
import {IStorageService, StorageScope} from 'vs/platform/storage/common/storage';

@Remotable.MainContext('MainThreadStorage')
export class MainThreadStorage {

    private _storageService: IStorageService;

    constructor( @IStorageService storageService: IStorageService) {
        this._storageService = storageService;
    }

I have never seen code using decorator syntax in this way ( before constructor's argument ), and I couldn't find typescript doc mentioning this usage.我从未见过以这种方式使用装饰器语法的代码(在构造函数的参数之前),而且我找不到提到这种用法的打字稿文档

Parameter decorators are documented at https://www.typescriptlang.org/docs/handbook/decorators.html#parameter-decorators参数装饰器记录在https://www.typescriptlang.org/docs/handbook/decorators.html#parameter-decorators

A Parameter Decorator is declared just before a parameter declaration.参数装饰器在参数声明之前声明。 The parameter decorator is applied to the function for a class constructor or method declaration.参数装饰器应用于类构造函数或方法声明的函数。 A parameter decorator cannot be used in a declaration file, an overload, or in any other ambient context (such as in a declare class).参数装饰器不能在声明文件、重载或任何其他环境上下文中使用(例如在声明类中)。

The expression for the parameter decorator will be called as a function at runtime, with the following three arguments:参数装饰器的表达式将在运行时作为函数调用,并带有以下三个参数:

  • Either the constructor function of the class for a static member, or the prototype of the class for an instance member.静态成员的类的构造函数,或者实例成员的类的原型。
  • The name of the member.成员的姓名。
  • The ordinal index of the parameter in the function's parameter list.函数参数列表中参数的序号索引。

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

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