新程序员的 TypeScript
祝贺您选择 TypeScript 作为您的首选语言之一——您已经做出了正确的决定!
您可能已经听说 TypeScript 是 JavaScript 的 "flavor" 或 "variant"。TypeScript (TS) 和 JavaScript (JS) 之间的关系在现代编程语言中相当独特,因此更多地了解这种关系将有助于您了解 TypeScript 如何添加到 JavaScript 中。
什么是 JavaScript?一个简短的历史
JavaScript(也称为 ECMAScript)最初是一种用于浏览器的简单脚本语言。在它被发明的时候,它被期望用于嵌入网页的短代码片段——编写超过几十行的代码有点不寻常。因此,早期的网络浏览器执行此类代码的速度非常慢。不过,随着时间的推移,JS 变得越来越流行,Web 开发人员开始使用它来创建交互式体验。
Web 浏览器开发人员通过优化他们的执行引擎(动态编译)和扩展它的功能(添加 API)来应对 JS 使用的增加,这反过来又使 Web 开发人员更多地使用它。在现代网站上,您的浏览器经常运行跨越数十万行代码的应用程序。这是 "the web" 漫长而渐进的成长过程,从一个简单的静态页面网络开始,逐渐演变成一个包含各种丰富应用程序的平台。
不仅如此,JS 已经变得足够流行,可以在浏览器上下文之外使用,例如使用 node.js 实现 JS 服务器。JS 的 "run anywhere" 特性使其成为跨平台开发的有吸引力的选择。现在有很多开发人员只使用 JavaScript 来编写他们的整个堆栈!
sk9bWU7Kd3BokB5Y8J0Obga/RLrgPPYRUXO9nR2pCoHRj4i8zlcaNQNgW7sBrpfO+5lq0szH4DEBAb45hmg42qHCcj62dIwu1Q4AB+l5WFdRaLRyQG/3suIGFrJK0xtU/k4OgEOW6Ctw/bGA2EZu1ZT73p5HDh3kYKd+yCFNVzgOcdV4JX3kATXz/Sl6FEKO4aSOk7RC+EIvG8f3P6rHECa3Jp8N3cRSuNLW/txZetOQdPS4/CsdlushtUW/im9kRD5r/7Bpr+fzQwA8Nf2twY5KyWr9ZAe5DCT3M6Vp0doOsJI/J1nIr4FWZEcw0/6i+E0msgpcvLmRT6OLL3P5qK4Xg/ArVcK3yQf+EwzRibgfw9ZVdOhtSL+PiomKAAiY2j6an95e6XvjMlY25uMciA==
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Script:静态类型检查器
0l/ZRD8EBvm76f22lzMrk/YH0Y9LMKdO6O/TyVfKEzRlQID1sHB9gNQ9C+ydnX5gv2LBygYmPws9+mJOjAw8ik0cmHRcKr/N8/W34b711jMsHdBS971cq+9J4syDF3wjUHZBcDfj275khcuR3Zj5kQexB1VPWKe6WTeFAv5fAviNkbxhgt3wYAg9AoxtTQSLzDJUgBp3RAnp8+X3+aatsNsPaQxKZDkyl5e1fOMrG9sVuEy/lC09tBWUwZGkLKOchz4SJaUym2nMk3bYNdFRW//GF6Qpo+q8kpKiCMfq2uhjOqa5xURlgu4zCuSNSAFgISfqHCc82olhDBnZaTADRTQKe7kVZSzQI2KN6awa700=
yb0ICxyVRHtPNcUf34eql5DQAA9eNzEL57newppWZej2Amg1UaK7APuVipnnYXXZQGG/53x4YquO6PiA/+U56wxbPnLtntyv8qESPeIvXoj9N8CBeZjKstdSGi9udJbu7Y4j4VbVRQz59wyTr13odKVW9cBcP9P7zxCWAHJ9ZIKyv7Umr1iMXRU5aNvzukjapPZq8m0SvyN0AiKxbNzsJbLjVcNUbuWDIzGdsiWaaBfY0/uSK33/zEoa0wkQwhF+9Q9OgCD7rvRE052xpEJN5qAN3YQghmxF9NjqYpmHgNKvQywUQJUXAPBJJugVIYRo3D3dr0cEz/0mnWI+rWuTew==
const obj = { width: 10, height: 15 };
const area = obj.width * obj.heigth;
类型化的 JavaScript 超集
O57ealVKxwNNZ4HjcASQVUIYrYqL899rr0djwZMqXT7+thGSZhSxWLs44kgmSa4U+NHw1zFMTPQhzmrTkUqIkQ==
语法
20GDeKLWhLCAWqlaQF4mwoPwnLr4o6hncrRm8DXPL7YgqECkR+5cGXxJ0L6mUOuHCySoOKNrqiAHhJtzdFA6qMNr68aTTDQ89gL8LCtzs7uKYrck6NLP88Moz6VdFK17W3ciOi0EaP2GPDjPmINip10im0j527xvx7LPDLj8+613pmSgdmc6HcvebG86ukOX8lWUJ0BmZI0H94H+pfXtXSVoS2LOZMKdcaQmB5n3nQG1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