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

Null and undefined

let a =[]
function valueCheck(array) {
    if (array == null) {
        console.log("Array==null");
    }
    if (array === undefined) {
        console.log("Array undefined");
    }
    if (!array) {
        console.log("!array");
        return;
    }
}
console.log("pass null");
valueCheck(null);
console.log("pass []");
valueCheck([]);
console.log("0 Check");
valueCheck(0);
valueCheck(false);
valueCheck();
Output:
pass null
Array==null
!array
pass []
0 Check
!array
!array
Array==null
Array undefined
!array

Symbol.toStringTag

const x = {
    [Symbol.toStringTag]: "X",
    a: "A",
    b: "B"
};
console.log(typeof Symbol.toStringTag === 'symbol');
console.log("Symbol.toStringTag in x: " + (Symbol.toStringTag in x));
console.log(Object.prototype.toString.call(x));
console.log("Symbol.toStringTag in Object(x): " + (Symbol.toStringTag in Object(x)));
console.log("a in Object(x): " + ("a" in Object(x)));
console.log("b in Object(x): " + ("b" in Object(x)));
console.log("c in Object(x): " + ("c" in Object(x)));
console.log("toStringTag: " + x[Symbol.toStringTag]);
let unmasked = false;
try {
    x[Symbol.toStringTag] = undefined;
    unmasked = true;
} catch (e){}
console.log("unmasked: " + unmasked);
console.log("toString: " + Object.prototype.toString.call(x));
try {
    x[Symbol.toStringTag] = "NewX";
} catch (e){}
console.log(Object.prototype.toString.call(Symbol.toStringTag));
console.log("toString: " + Object.prototype.toString.call(x));
Output:
true
Symbol.toStringTag in x: true
[object X]
Symbol.toStringTag in Object(x): true
a in Object(x): true
b in Object(x): true
c in Object(x): false
toStringTag: X
unmasked: true
toString: [object Object]
[object Symbol]
toString: [object NewX]