lundi 24 juin 2019

understanding notation: const { someVar } = otherVar [duplicate]

The more Javascript code I read in real working code the more quirks of notation I can't seem to wrap my head around I've never seen in documentation. The following example is from the top of the node.js lib http.js.

const { Object } = primordials;

Knowing from whence it came, however, has done little to help me understand what it means; either how it works, or how it is an advantage because of the way it does.

So, I cobbled a time wasting thing (below) I was hoping might help me understand what's going on, but it just made me more confused than when I started the inquiry.

My question is twofold. What is more accepted way to investigate bits of code I don't understand like the one presented, and the second part, will you explain what that method reveals about the sample's meaning and use?

One possible clue for me is the error message, something to do with destructuring, but it still didn't seem to help, and following that path hasn't so far gotten me to a simpler way. Is it alright that when messing with JS, I get nostalgic for pydoc?

In the comments of my attempt, I've included a couple of half-remembered notations which, to my mind, are found in the most fluent and highly regarded of code I've read that, for their inclusion, I have trouble understanding.

// const { Object } = primordials;

const y = {key: 'birthday', val: 99}
const { x } = y
simple(x) // undefined
simple(y) // object y

const n = 88
const { m } = n
simple(m) // undefined
simple(n) // 88

const q = function(){console.log(77);}
const { p } = q
simple(p) // undefined
simple(q) // function q

const b = null
// const { a } = b // error: Cannot destructure property `a` of 'undefined' or 'null'
// simple(a)
simple(b) // null

function simple (z) {
    console.log(z);

}

// !!somevar = 'a_val'
// [someReference] 

Aucun commentaire:

Enregistrer un commentaire