简体   繁体   English

创建对象/函数实例,将JavaScript转换为TypeScript

[英]Create instance of object/function convert JavaScript to TypeScript

Having this function I need to create a new instance of it. 具有此功能,我需要为其创建一个新实例。 Everything works fine in JavaScript but how to I convert it to TypeScript? 在JavaScript中一切正常,但是如何将其转换为TypeScript?

 function Calendar(selector, events) { this.el = document.querySelector(selector); this.events = events; this.current = moment().date(1); this.draw(); var current = document.querySelector('.today'); if(current) { var self = this; window.setTimeout(function() { self.openDay(current); }, 500); } } 

 var calendar = new Calendar('#calendar', data); 

 var calendar = new Calendar('#calendar', data); 

It is true that anything that works in JavaScript will work in TypeScript, but that just means that the TypeScript compiler will output your JavaScript more or less untouched, possibly spitting out a bunch of warnings on the way. 的确,任何可以在JavaScript中工作的东西都可以在TypeScript中工作,但这仅意味着TypeScript编译器将或多或少地输出JavaScript,这可能会在途中吐出一堆警告。 If you just ignore the errors, things will still work. 如果您只是忽略这些错误,那么一切仍然可以进行。

But assuming you want to leverage the power of TypeScript, you should start changing things. 但是,假设您想利用TypeScript的功能,则应该开始进行更改。 Let's start. 开始吧。


First, you should install the typings from Moment.js in your project, probably by running npm install moment from your project folder. 首先,您应该从项目文件夹中运行npm install moment从项目中的Moment.js npm install moment

Then, I usually like to turn on all the --strictXXX compiler flags (I think you can just use --strict ) to get the maximum number of warnings to ignore and/or fix. 然后,我通常喜欢打开所有--strictXXX编译器标志(我想您可以只使用--strict )来获取要忽略和/或修复的最大警告数。

Okay, now: the ES6/TypeScript idiom for a constructible thing is to use a class . 好的,现在:ES6 / TypeScript惯用语是使用class Here's a look at some modifications I made, with some inline comments: 以下是我所做的一些修改以及一些内联注释:

import * as moment from 'moment';

class Calendar {
  // a Calendar has an el property which is a possibly null DOM element:
  el: Element | null; 
  // a Calendar has a current property which is a Moment:
  current: moment.Moment;
  // a Calendar has an events property which is an array of Event:
  events: Event[];

  // the constructor function is what gets called when you do new Calendar()
  // note that I assume selector is a string and events is an array of Event
  constructor(selector: string, events: Event[]) {
    this.el = document.querySelector(selector);  
    this.events = events;  
    this.current = moment().date(1);
    this.draw(); 
    var current = document.querySelector('.today');
    if (current) {
      var self = this;
      window.setTimeout(function() {
        self.openDay(current); 
      }, 500);
    }
  }

  draw() {
    // needs an implementation
  }

  openDay(day: Element | null) {
    // needs an implementation
  }
}

declare let data: Event[]; // need to define data
var calendar = new Calendar('#calendar', data);

You need to implement the draw() and openDay() methods which are presumably part of the Calendar.prototype . 您需要实现draw()openDay()方法,它们大概是Calendar.prototype一部分。 I put stubs for them in there. 我在那儿放了存根给他们。 You also need to define data , which is (I'm guessing) an array of events (if it's something else you need to change the type of events . 您还需要定义data ,它是(我正在猜测)事件数组(如果还有其他事情,您需要更改events的类型)。

If you look at the compiled JavaScript output from the above, you'll see that it's more or less the same as what you had. 如果您看一下上面的编译后的JavaScript输出,您会发现它与您所拥有的或多或少相同。 But now, of course, TypeScript is happy to let you call new Calendar(...) . 但是,现在,当然,TypeScript很高兴让您调用new Calendar(...)


There are more changes you can make, of course. 当然,您可以进行更多更改。 For example, you can use parameter properties and remove the this.events = events; 例如,您可以使用参数属性并删除this.events = events; line. 线。 Or you can use property initializers and move the this.current = ... out of the constructor function and into the property declaration. 或者,您可以使用属性初始化程序,并将this.current = ...constructor函数中移出,并移入属性声明中。 Et cetera. 等等。

But this should hopefully be enough to get you started. 但这应该足以让您入门。 Good luck! 祝好运!

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

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