dive2Pro / Think

_博客_
0 stars 0 forks source link

Javascript #4

Open dive2Pro opened 7 years ago

dive2Pro commented 7 years ago

in javascript variables are tied to the arguments object so changing the variables changes arguments and changing arguments changes the local variables even when they are not in the same scope.

dive2Pro commented 7 years ago

[].reverse will return this and when invoked without an explicit receiver object it will default to the default this AKA window

dive2Pro commented 7 years ago

Number.MIN_VALUE is the smallest value bigger than zero, -Number.MAX_VALUE gets you a reference to something like the most negative number.

dive2Pro commented 7 years ago

Per spec Two regular expression literals in a program evaluate to regular expression objects that never compare as === to each other even if the two literals' contents are identical.

dive2Pro commented 7 years ago

var a = [1, 2, 3], b = [1, 2, 3], c = [1, 2, 4] a == b a === b a > c a < c

// Arrays are compared lexicographically with > and <, but not with == and ===

dive2Pro commented 7 years ago

function f() {} var a = f.prototype, b = Object.getPrototypeOf(f); a === b

f.prototype is the object that will become the parent of any objects created with new f while Object.getPrototypeOf returns the parent in the inheritance hierarchy.

dive2Pro commented 7 years ago

name is a read only property. Why it doesn't throw an error when assigned, I do not know.

dive2Pro commented 7 years ago

"1 2 3".replace(/\d/g, parseInt)

String.prototype.replace invokes the callback function with multiple arguments where the first is the match, then there is one argument for each capturing group, then there is the offset of the matched substring and finally the original string itself. so parseInt will be invoked with arguments [1, 0], [2, 2], [3, 4].

dive2Pro commented 7 years ago

function f() {} var parent = Object.getPrototypeOf(f); f.name // ? parent.name // ? typeof eval(f.name) // ? typeof eval(parent.name) // ?

The function prototype object is defined somewhere, has a name, can be invoked, but it's not in the current scope.

dive2Pro commented 7 years ago

Function.length is defined to be 1. On the other hand, the length property of the Function prototype object is defined to be 0.

dive2Pro commented 7 years ago

Math.min returns +Infinity when supplied an empty argument list. Likewise, Math.max returns -Infinity.

dive2Pro commented 7 years ago

function foo(a) { var a; return a; } function bar(a) { var a = 'bye'; return a; } [foo('hello'), bar('hello')]

Variabled declarations are hoisted, but in this case since the variable exists already in the scope, they are removed altogether. In bar() the variable declaration is removed but the assignment remains, so it has effect.