打字游戏代码

    科技2022-08-01  115

    打字游戏代码

    JavaScript与TypeScript (JavaScript vs TypeScript)

    JavaScript started its journey a few decades back by doing simple dynamic things on web pages. But now JavaScript has very useful features such as classes support and powerful standard built-in objects like any other modern programming language. JavaScript is a dynamically typed programming language because the whole concept is to execute a source script in the user’s sandboxed environment via an interpreted way.

    JavaScript通过在网页上进行简单的动态操作开始了几十年的旅程。 但是现在JavaScript具有非常有用的功能,例如类支持和功能强大的标准内置对象,就像任何其他现代编程语言一样。 JavaScript是一种动态类型化的编程语言,因为整个概念是通过解释的方式在用户的沙盒环境中执行源脚本。

    TypeScript is very popular because it introduced a new way that will help developers to write JavaScript using the statically typed concept. So it’s easy to identify data types before the execution of the code.

    TypeScript非常受欢迎,因为它引入了一种新方法,可以帮助开发人员使用静态类型的概念来编写JavaScript。 因此,在执行代码之前很容易识别数据类型。

    Let’s jump into the tips and tricks section!

    让我们跳到提示和技巧部分!

    交叉点和联合 (Intersection and Union)

    There are situations where we need to pass different types of variables with the same parameter of a function and also where we need to combine properties of multiple variable types.

    在某些情况下,我们需要使用函数的相同参数传递不同类型的变量,并且还需要组合多个变量类型的属性。

    Union: Helpful when a specific function parameter supports multiple variable types.

    Union :当特定的函数参数支持多种变量类型时很有用。

    Intersection: Merges two variable types into a single type which will have all the properties of given variable types.

    交集 :将两个变量类型合并为一个类型,该类型将具有给定变量类型的所有属性。

    魔术或 (Magic OR)

    If there are several variables and there is a need to set the truthy value to another variable, it could be simply solved using a couple of if conditions. But how about using || instead for more cleaner and wise code?

    如果存在多个变量,并且需要将真实值设置为另一个变量,则可以使用几个if条件简单地解决。 但是如何使用|| 而不是更干净,更明智的代码?

    抽象属性 (Abstract properties)

    We all are familiar with abstract classes because we often use that concept to achieve well maintainable code using object oriented design principles. TypeScript supports abstract properties as well therefore we are able to define some abstract properties in a non-concrete class and use it via assigning some value in a concrete class.

    我们都熟悉抽象类,因为我们经常使用该概念来使用面向对象的设计原理来实现可维护性良好的代码。 TypeScript也支持抽象属性,因此我们能够在非具体类中定义一些抽象属性,并通过在具体类中分配一些值来使用它。

    外部库定义 (External library definitions)

    Some TypeScript projects may use non-typescript global libraries. Our old friend Jquery is a good example. If the particular external library has TypeScript definitions there will be no issues with transpilation. Whereas if the particular external library is a custom one or not openly available one you can get away from transpilation errors using following tips.

    一些TypeScript项目可能使用非Typescript全局库。 我们的老朋友Jquery是一个很好的例子。 如果特定的外部库具有TypeScript定义,则编译不会有问题。 而如果特定的外部库是自定义的或未公开可用的,则可以使用以下提示来避免出现翻译错误。

    The lazy way

    懒惰的方式

    Nothing but declaring the whole external library as any. For an example declaring $ of Jquery as declare let $: any

    只能将整个外部库声明为任意 。 例如,将Jquery的$声明$ define declare let $: any

    The active way

    主动方式

    More correct way is to create your own TypeScript definitions for the external library (if it’s not publicly available). Simply create index.d.ts with the required definitions using interfaces or namespaces. For a simple example Typescript definitions of Neutralinojs can be found here

    更正确的方法是为外部库创建自己的TypeScript定义(如果它不是公开的)。 只需使用接口或名称空间创建具有所需定义的index.d.ts 。 对于一个简单的示例,可以在此处找到Neutralinojs的 Typescript定义

    您简单的条件是否可能出错 (Your simple if condition could be wrong)

    Developers mostly use if(variable)and if(!variable) to check whether a particular variable is available to use it or not. Importantly 0 is not a truthy value in Javascript. Therefore, we need to be careful with truthy number checks.

    开发人员通常使用if(variable)和if(!variable)来检查特定变量是否可用。 重要的是, 0不是Javascript中的真实值。 因此,我们需要谨慎进行真实数字检查。

    In the above example, if condition is not behaving correctly because itemIndex could be 0 too. Therefore if we modify the condition right to be on the point like if(item.itemIndex != null) everything will start behaving correctly as we expected.

    在上面的示例中, if condition的行为不正确,因为itemIndex也可能为0 。 因此,如果我们将条件权修改为类似if(item.itemIndex != null)类的点, if(item.itemIndex != null)一切将按预期正确开始。

    Happy coding everyone!😎

    祝大家编码愉快!😎

    普通英语JavaScript (JavaScript In Plain English)

    Did you know that we have three publications and a YouTube channel? Find links to everything at plainenglish.io!

    您知道我们有三个出版物和一个YouTube频道吗? 在plainenglish.io上找到所有内容的链接!

    翻译自: https://medium.com/javascript-in-plain-english/top-5-pro-tips-for-your-next-typescript-code-33ae2c6f1e5e

    打字游戏代码

    相关资源:JAVA上百实例源码以及开源项目源代码
    Processed: 0.011, SQL: 8