Reflect In JavaScript

Reflect in JavaScript

Many of the functions for general operations are distributed across a bunch of objects and keywords in JavaScript. This increases the cognitive load and increases the number of times a language operation needs to be looked up. Here are a few quick examples:

const obj = { property1: 'value1', property2: 'value2', }; delete obj.property1; Object.preventExtensions(obj); try { obj.property3 = 'value3' } catch(e) { console.log(e.message); } console.log(obj); // { property2: 'value2' }

Reflect simplifies this by providing a consistent API for all of the operations:

const obj2 = { property1: 'value1', property2: 'value2', }; Reflect.deleteProperty(obj2, 'property1'); Reflect.preventExtensions(obj2); const succeeded = Reflect.set(obj2, 'property3', 'value3'); if (succeeded !== true) { console.log('Failed to set "property3", obj is not extensible'); } console.log(obj2); // { property2: 'value2' }

Notice how Reflect.set does not return an error and returns if the value was set instead. Even for basic operations, reflect can help clarify the code more

Those were just a few examples, you can view the full list here. Since Reflect provides a consistent API for many operations, it should generally be used instead of all the separate interfaces when it is available. There are exceptions when the syntax is more clear though; except for cases like the above, setting variables with obj.a = 'value' is usually clearer than Reflect.set(obj, 'a', 'value'). It is supported in all browsers including MS Edge, but not IE 11.

Github Location: https://github.com/Jacob-Friesen/obscurejs/blob/master/2017/reflect.js

More Posts from Techfair-blog1 and Others

7 years ago

Working on it.

The US Desperately Needs Computer Science Majors, So Keep Coding

The US desperately needs computer science majors, so keep coding

The United States is paradoxically home to some of the world’s biggest tech companies and yet doesn’t have enough people studying computer science to fill those jobs.

The U.S. will have 1 million open computing jobs by 2024, according to a recent report by the App Association.

In 2013, the Obama Administration issued a similar statistic estimating that there would be 1.4 million computer science jobs even sooner — by 2020 — but fewer than 400,000 people will graduate with the right skills to fill them. Read more (7/21/17)

follow @the-future-now​


Tags
7 years ago
Exclusive Rae Sremmurd From Lollapalooza… 
Exclusive Rae Sremmurd From Lollapalooza… 
Exclusive Rae Sremmurd From Lollapalooza… 

Exclusive Rae Sremmurd from Lollapalooza… 

via Drew Levin

7 years ago

how to get all 3 new post fonts

most of y’all have seen the new cursive font, but there’s actually three new fonts available for regular text posts. I’m gonna tell you how to use them on the desktop site and how to add them to your Tumblr theme.

1. Fairwater (Quirky)

click the gear in the top-right of posts and select “HTML” from “Text editor”. Then just type <p class=“npf_quirky”>Hello!</p> and replace the text with whatever you want.

to put it in your Tumblr theme, go to your customize page > Advanced options > ADD CUSTOM CSS and paste this code:

p.npf_quirky { font-family: Fairwater, serif; font-size: 24px; line-height: 1.3em; } @font-face{font-family:Fairwater; src:url("http://static.tumblr.com/zgb0z1w/eKLov1ol9/fairwater_script_regular-webfont.woff2?v=b7ab7a58cded3365889a447bfd9e9c45") format("woff2"),url("http://static.tumblr.com/zgb0z1w/Ia8ov1pxd/fairwater_script_regular-webfont.woff?v=96e975b7468359e6f67086305577e43e") format("woff"); font-weight:400; font-style:normal}

2. Calluna (Quote)

click the gear in the top-right of posts and select “HTML” from “Text editor”. Then just type <p class=“npf_quote”>Hello!</p> and replace the text with whatever you want.

to put it in your Tumblr theme, go to your customize page > Advanced options > ADD CUSTOM CSS and paste this code:

p.npf_quote { font-family: Calluna, sans-serif; font-size: 14px; } @font-face{font-family:Calluna; src:url("http://static.tumblr.com/zgb0z1w/Pjnov1py3/calluna-regular-webfont.eot?3=&v=7c192d86344b943cd3e8d9123ed1dafa");src:url("http://static.tumblr.com/zgb0z1w/Pjnov1py3/calluna-regular-webfont.eot?3%3F=&v=7c192d86344b943cd3e8d9123ed1dafa#iefix") format("embedded-opentype"),url("http://static.tumblr.com/zgb0z1w/ALjov1pzw/calluna-regular-webfont.woff?3=&v=60172bcdc91d310108023e102eec8676") format("woff"),url("http://static.tumblr.com/zgb0z1w/HsIov1q0g/calluna-regular-webfont.ttf?3=&v=cc6292d625d03dc65b9665f0cd27e2cd") format("truetype"); font-weight:400; font-style:normal}

3. Courier (Chat)

click the gear in the top-right of posts and select “HTML” from “Text editor”. Then just type <p class=“npf_chat”>Hello!</p> and replace the text with whatever you want.

to put it in your Tumblr theme, go to your customize page > Advanced options > ADD CUSTOM CSS and paste this code:

p.npf_chat { font-family: Courier, monospace; font-size: 14px; }

have fun with the new fonts!

7 years ago
Someone Take Gorn Away From Him Before He Loses His Mind
Someone Take Gorn Away From Him Before He Loses His Mind

someone take Gorn away from him before he loses his mind

7 years ago
Meme Of The Day.

Meme of the day.

Thank you to @djmakproto for the source link, which I could not previously find!

7 years ago
The Vesta Explorer In Strange New World (USA, 1975 Dir: Robert Butler).

The Vesta Explorer in Strange New World (USA, 1975 dir: Robert Butler).

7 years ago
Check It Out:  Https://pennweallotu1975.tumblr.com/post/164528177008/fiber-optic-hoodie-lazyop

Check it out:  https://pennweallotu1975.tumblr.com/post/164528177008/fiber-optic-hoodie-lazyop

  • i-iii-iii-vii
    i-iii-iii-vii liked this · 6 years ago
  • photrex-blog
    photrex-blog liked this · 7 years ago
  • webgodo-blog
    webgodo-blog liked this · 7 years ago
  • techfair-blog1
    techfair-blog1 reblogged this · 7 years ago
  • blueluminaryqueen-blog
    blueluminaryqueen-blog liked this · 7 years ago
  • muditash
    muditash liked this · 7 years ago
  • sakipgr
    sakipgr liked this · 7 years ago
  • rakomeloontherocks
    rakomeloontherocks reblogged this · 7 years ago
  • rakomeloontherocks
    rakomeloontherocks liked this · 7 years ago
  • update-package-reinstall
    update-package-reinstall reblogged this · 7 years ago
  • inflorescens
    inflorescens liked this · 7 years ago
  • wimagine
    wimagine liked this · 7 years ago
  • obscurejavascript
    obscurejavascript reblogged this · 7 years ago
techfair-blog1 - Tech Fairy
Tech Fairy

Expanding my passion for tech.

13 posts

Explore Tumblr Blog
Search Through Tumblr Tags