GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing basarat.gitbook.io — Destructuring | TypeScript Deep Dive TypeScript supports the following forms of Destructuring (literally named after de-structuring i.e. breaking up the structure):
It is easy to think of destructuring as an inverse of structuring. The method of structuring in JavaScript is the object literal: Without the awesome structuring support built into JavaScript, creating new objects on the fly would indeed be very cumbersome. Destructuring brings the same level of convenience to getting data out of a structure. Object DestructuringDestructuring is useful because it allows you to do in a single line, what would otherwise require multiple lines. Consider the following case: Here in the absence of destructuring you would have to pick off To assign an extracted variable to a new variable name you can do the following: Additionally you can get deep data out of a structure using destructuring. This is shown in the following example: Object Destructuring with restYou can pick up any number of elements from an object and get an object of the remaining elements using object destructuring with rest. A common use case is also to ignore certain properties. For example: Array DestructuringA common programming question: "How to swap two variables without using a third one?". The TypeScript solution: Note that array destructuring is effectively the compiler doing the Array Destructuring with restYou can pick up any number of elements from an array and get an array of the remaining elements using array destructuring with rest. Array Destructuring with ignoresYou can ignore any index by simply leaving its location empty i.e. JS GenerationThe JavaScript generation for non ES6 targets simply involves creating temporary variables, just like you would have to do yourself without native language support for destructuring e.g. SummaryDestructuring can make your code more readable and maintainable by reducing the line count and making the intent clear. Array destructuring can allow you to use arrays as though they were tuples. Last updated |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |