Saturday, January 27, 2018

windows

const freeSelf = typeof self == 'object' && self !== null && self.Object === Object && self
console.log(freeSelf)
const g = Function('return this')();
console.log("this " + g)
console.log(window === window.self)
console.log("Is g === self? " + (g === self))
Output:
Window
this [object Window]
true
Is g === self? true

Function

Function('return this')()

arguments object

const a = function (a, b) {
    console.log(Object.prototype.toString.call(arguments));
}
a(2, 3)
Output:
[object Arguments]

Friday, January 26, 2018

map

const a = [2, 4]
console.log(a)
console.log(`${a}`)
console.log(`${a.map((v)=>{return 2*v})}`)
Output:
[ 2, 4 ]
2,4
4,8

NaN and Infinitiy

console.log(1/0)
console.log(-1/0)
console.log(0/0)
console.log(-0/0)
console.log("Using special constants")
console.log(NaN)
console.log(Infinity)
const n0 = -0
console.log(`${n0}`)
console.log(1/n0 == -Infinity ? '-0' : n0)
Output:
Infinity
-Infinity
NaN
NaN
Using special constants
NaN
Infinity
0
-0

Sunday, January 21, 2018

regex

var str = "/* First comment */\n/* another comment */";
var patt1 = /\/\*[\s\S]*?\*\//mg;
var result = str.match(patt1);
console.log("result is: " + result);
var patt2 = /([^:"'=])/g;
var str2 = "p:\"Hi\"...'Hi' x=y";
console.log(str2 + "->" + str2.match(patt2));
var  jsSuffixRegExp = /\.js$/;
console.log("app.js".replace(jsSuffixRegExp, ''));
Output:
result is: /* First comment */,/* another comment */
p:"Hi"...'Hi' x=y->p,H,i,.,.,.,H,i, ,x,y
app

Number

const n = "2";
const m = +n;
console.log("m: " + m);
console.log(typeof m);
console.log('2' + '3')
console.log('2' * '3')
Output:
true
false
true
23
6