today-i-learned
Today, I learned ...
this
in javascript anonymous functionposition:relative
==
equality & value conversion in javascript:nth-child(odd)
&:nth-child(even)
{this.props.children}
pass a react component to another- use
Host
to identify whichid_rsa.pub
is to be used. defer
&async
in<script>
- checkbox - intermediate state
patch
command[
vs[[
- modify an older (not latest) commit message
matching
vscurrent
push.default
ingit
node filename
is sufficient.js
is not required<button>
is better than<a>
for custom widgets- Vim - save a file with
sudo
document.createElementNS
- Override a node module's function
npm ls <package-name>
to list the dependency tree:after
&:before
can not be used forinput
element- start a
ordered list
from a desired ordinal value !important
in a keyframeoutline-offset
border
andoutline
styles can not be animated- listen to
focus
andblur
during capture phase - remove all the untracked files
- element.closest()
- destructuring default param
Cannot find module 'internal/fs'
this
in javascript anonymous function
If using 'use strict';
it's undefined
other wise global this
, Window
in browser.
(function() {
'use strict';
console.log(this); // undefined
}());
(function() {
console.log(this); //[object global]
}());
However, in ES2015 arrow function, this
is the lexical this
.
position:relative
position:relative
means theposition
of theelement
is relative to itself- if there are no associated,
left
,right
,top
,bottom
,position:relative
does not have any effect for the element's layout - unlike
position:absolute|fixed
, the element is not taken out of the flow. - codepen example
==
equality & value conversion in javascript
- in
==
both the values are converted into a common type prior comparison. - for
number == string
orstring == number
string
is convereted tonumber
- more conversion rules can be found here
:nth-child(odd)
& :nth-child(even)
:nth-child(2n + 1)
is same as:nth-child(odd)
:nth-child(2n)
is same as:nth-child(even)
{this.props.children}
pass a react component to another
export default class Dumb extends React.Component {
render() {
return (
<div>
Hi from Dumb
{this.props.children}
</div>
);
}
}
export default class AnotherDumb extends React.Component {
render() {
return <span>Hello from AnotherDumb</span>;
}
}
ReactDOMServer.renderToStaticMarkup(<Dumb><AnotherDumb /></Dumb>);
// or
ReactDOMServer.renderToStaticMarkup(
React.createElement(
Dumb, {}, React.createElement(AnotherDumb)
)
);
<div>
Hi from Dumb
<span>
Hello from AnotherDumb
</span>
</div>
Host
to identify which id_rsa.pub
is to be used.
use Host *.example.one*
IdentityFile %d/.ssh/id_rsa_example_one.pub
Host *.example.two*
IdentityFile %d/.ssh/id_rsa_example_two.pub
defer
& async
in <script>
- when
async
attribute is present, the script is fetched w/o blocking HTML parsing and executed as soon as it is available. While executing the HTML parsing will be blocked. - when
defer
attribute is present, w/oasync
the script will be fetched w/o blocking HTML parsing and only executed, when the page has finished parsing. - without
async
ordefer
, the script is fetched blocking the HTML parsing and executed immediately. - When
async
anddefer
both are presentdefer
serves as a fall back for the browsers that don't understandasync
.
checkbox - intermediate state
intermediate state can only be set with javascript
var checkbox = document.getElementById("some-checkbox");
checkbox.indeterminate = true;
patch
command
patch < patch-file
for example
吽 echo hi\\nhello > 1.txt
吽 echo Hi\\nHello > 2.txt
吽 diff -u 1.txt 2.txt > patch.txt
[zsh] sabandyo at sabandyo-mn1 in ~
吽 cat 1.txt
hi
hello
吽 cat patch.txt
--- 1.txt 2016-03-24 18:56:38.000000000 -0700
+++ 2.txt 2016-03-24 18:56:32.000000000 -0700
@@ -1,2 +1,2 @@
-hi
-hello
+Hi
+Hello
吽 patch < patch.txt
patching file 1.txt
吽 cat 1.txt
Hi
Hello
[
vs [[
-
[[
- is more powerful
- is only supported in modern
shell
likebash
,zsh
- is not available in
POSIX shells
- is safer
- use
#!/bin/bash
or#!/bin/zsh
when using[[
-
[
var
s containingspaces
nedds to be wrapped in"
More information can be found at this StackOverflow answer
modify an older (not latest) commit message
git rebase -i HEAD~10
for example if the 10th commit from the latest needs to be modified- replace
pick
withedit
for the corresponding commit git commit --amend
- update the commit message
- `git rebase --continue
matching
vs current
push.default
in git
-
matching
meansgit push
willpush
all local branches, to the ones with the same name on the remote. This makes it easy to accidentally push a non-intended branch. -
simple
meansgit push
will push only the current branch, to the one thatgit pull
would pull from, and also checks that their names match. This is a more intuitive behavior, which is why the default is getting changed to this.
node filename
is sufficient .js
is not required
<button>
is better than <a>
for custom widgets
<a> must be accompanied with
href- otherwise
click
handler would not listen toenter
press - and it will not be focusable with
tab
- otherwise
<a>
does not respectspace
press (at least in chrome/safari/firefox on Mac)- for
<a>
,e.prevenDefault()
must be used in theclick
handler - both of them need to have widget
role
attribute, if not serving the native semantic
Refer this CodePen
sudo
Vim - save a file with I haven't learnt this today, but I keep forgetting it
w !sudo tee %
document.createElementNS
document.createElementNS(namespaceURI, qualifiedName);
creates an element with the specified namespace URI and qualified name.
For example document.createElementNS('http://www.w3.org/2000/svg', 'svg')
Override a node module's function
// fs.js
const fs = require('fs');
const readFileSync = fs.readFileSync;
fs.readFileSync = (...args) => {
const content = readFileSync.apply(null, args);
console.log(`read ${args[0]} in sync`)
console.log(`constent of ${args[0]}\n----------------------\n${content}`);
return content;
}
// app.js
const fs = require('fs');
require('./fs');
fs.readFileSync('./fs.js', 'utf-8');
$ node app.js
read ./fs.js in sync
constent of ./fs.js
----------------------
const fs = require('fs');
const readFileSync = fs.readFileSync;
fs.readFileSync = (...args) => {
const content = readFileSync.apply(null, args);
console.log(`read ${args[0]} in sync`)
console.log(`constent of ${args[0]}\n----------------------\n${content}`);
return content;
}
npm ls <package-name>
to list the dependency tree
$ npm ls trim
...
└─┬ doctoc@1.0.0
└─┬ markdown-to-ast@3.0.7
└─┬ mdast@1.2.0
└── trim@0.0.1
:after
& :before
can not be used for input
element
It's not meant to be used on replaced elements such as form elements (inputs) and image elements.
ordered list
from a desired ordinal value
start a
<ol>
<li start="4" value="4">item 4</li>
<li>item 5</li>
<li>item 6</li>
</ol>
!important
in a keyframe
declarations in a keyframe that are qualified with !important are ignored
outline-offset
outline-offset
is a thing! and it can have -ve
value
border
and outline
styles can not be animated
This is by design, read more at this stackoverflow answer
focus
and blur
during capture phase
listen to focus
& blur
event don't bubble up. So event listener attached to the ancestor can not listen to focus
and blur
event on its descendant
as it can for click
event.
document.addEventListener('click', () => {console.log(e.target, 'is clicked')}); // logs the element clicked
document.addEventListener('focus', () => {console.log(e.target)}); // however, this does not work
document.addEventListener('focus', () => {console.log(e.target)}, true); // but, this work
Note the true
flag passed as the 3rd argument, in the last statement. true
identifies it to be capture
phase.
remove all the untracked files
git clean -fd
will remove all the untracked files
element.closest()
Native method, not jQuery, that returns the closest ancestor of the current element.
destructuring default param
function foobar({
foo = 'foo',
bar = 'bar'
} = {}) {
console.log(foo);
console.log(bar);
}
foobar()
// foo
// bar
foobar({
foo: 'oof',
bar: 'rab'
})
// oof
// rab
refer MDN for more information.
Cannot find module 'internal/fs'
Fix Cannot find module 'internal/fs'
error with n
and node >= 7
, like so
$ n 6
$ npm i -g npm@next
$ n latest
Source - npm issue comment