This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Examples of JS Object.freeze function (mdn: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze) | |
| let o = { | |
| name: 'todd', | |
| age: 34, | |
| favoriteLanguage: 'JavaScript' | |
| }; | |
| Object.freeze(o); | |
| o.newProp = 'this should not get added'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //JavaScript Objects and Prototypes: Two ways of understanding prototypes | |
| //Constructor function | |
| function Cat(name, color) { | |
| this.name = name; | |
| this.color = color; | |
| //this.age = 7; | |
| } | |
| console.log(Cat.prototype); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Original logic for reference: | |
| component ? ( // component prop gets first priority, only called if there's a match | |
| match ? React.createElement(component, props) : null | |
| ) : render ? ( // render prop is next, only called if there's a match | |
| match ? render(props) : null | |
| ) : children ? ( // children come last, always called | |
| typeof children === 'function' ? ( | |
| children(props) | |
| ) : !Array.isArray(children) || children.length ? ( // Preact defaults to empty children array | |
| React.Children.only(children) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const baseArray = [1, 2, 3, 4]; | |
| const newArr = Object.assign([...baseArray], {2: 5}); | |
| console.log(newArr); // [1, 2, 5, 4] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export default function () { | |
| return { | |
| visitor: { | |
| VariableDeclaration(path) { | |
| path.node.declarations[0].id.name = 'TransformedInVariableDeclaration'; | |
| const template = path.node.declarations[0].init.quasis[0].value.raw; | |
| console.log(template); | |
| }, | |
| TemplateLiteral(path) { | |
| const template = path.node.quasis[0].value.raw; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //copy this into AST Explorer and run it against a standard import statement, like import React from 'react'; | |
| export default function ({types: t}) { | |
| return { | |
| visitor: { | |
| ImportDeclaration(path) { | |
| const {node} = path; | |
| path.replaceWith( | |
| t.variableDeclaration('var', [ | |
| t.variableDeclarator(t.identifier(`_${node.specifiers[0].local.name}`), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Should transform: import {Component} from 'react'; | |
| //to: const _Component = require('react').Component; | |
| export default function ({types: t}) { | |
| return { | |
| visitor: { | |
| ImportDeclaration(path) { | |
| const {node} = path; | |
| path.replaceWith( | |
| t.variableDeclaration('var', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*Object.create() vs new constructor(): constructor will have __proto__ and a "prototype" property, but a regular object will only have __proto__*/ | |
| //Object.create() just creates a new object and assigns it the prototype that you provide as the first argument | |
| const person = { | |
| name: 'Name', | |
| age: 100, | |
| sayHello: function () { | |
| console.log(`Hello, my name is ${this.name} and I am ${this.age} years old`); | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const numbers = [1,2,3,4,5,6,7,8]; | |
| Array.prototype.logValues = function() { | |
| for (i = 0; i < this.length; i++) { | |
| console.log(this[i]); | |
| } | |
| }; | |
| numbers.logValues(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "editor.wrappingColumn": 0, | |
| "typescript.check.tscVersion": false, | |
| "workbench.colorTheme": "Operator Mono Dark Theme", | |
| "editor.tabSize": 2, | |
| "workbench.iconTheme": "vscode-icons", | |
| "[javascript]": { "editor.tabSize": 2, "editor.insertSpaces": false, "editor.detectIndentation": false } | |
| } |
OlderNewer