其他
JavaScript 中的“黑话”,你知多少?
作者:vv13 https://segmentfault.com/a/1190000016595592
因为球是圆的,所以不论发生什么都有可能,对这点我是深信不疑的,但最近我总是在怀疑,JavaScript也是圆的!
什么是“黑话”
“算术”
!与!!
将一个值转化为布尔值; 将其取反; 再次取反。
const enable1 = !!id;
const enable2 = id ? true: false;
const enable3 = Boolean(id);
~ 与 ~~
转为一个字节的二进制表示:00000101, 按位取反:11111010 取其反码:10000101 取其补码:10000110 转化为十进制:-6
~~null; // => 0
~~undefined; // => 0
~~Infinity; // => 0
--NaN; // => 0
~~0; // => 0
~~{}; // => 0
~~[]; // => 0
~~(1/0); // => 0
~~false; // => 0
~~true; // => 1
~~1.9; // => 1
~~-1.9; // => -1
+
+'1'// 1
+'-1'// '-1
+[] // 0
+{} // NaN
& 与 &&
先转换为2进制:111 & 11 比较结果为:011 将二进制转回十进制,因此:7 & 3 = 3
若第一个表达式为false,则返回第一个表达式; 若第一个表达式为true,返回第二个表达式。
0&& false0(both are false-y, but 0is the first)
true&& falsefalse(second one isfalse-y)
true&& truetrue(both are true-y)
true&& 2020(both are true-y)
const value = obj && obj.value || false
// before
if(test) { alert('hello') }
// after
test && alert('hello')
| 与 ||
// before
let res;
if(a) {
res = a;
} elseif(b) {
res = b;
} elseif(c) {
res = c;
} else{
res = 1;
}
// after
const res = a || b || c || 1;
== 与 ===
===
为全等运算符,它除了在比较时不会将操作数强制转型,其余相等判断与==一致。==
用于判断值是否相等, ===
判断值与类型是否都相等,因此使用全等运算符判断操作数会更准确,新手也在学习JavaScript接收到的前几条Tips就是避免使用相等运算符,真的是这样吗?没错,这样能确保在你不彻底熟悉语言的情况下,尽可能的去避免犯错,但是我们也应该清楚在哪些情况下应该使用相等运算符,规则往往只针对于新手,而对聪明的你来说,最重要的是要清楚自己在做什么。if(VAR == undefined) {}
if(VAR == null) {}
if(VAR === undefined|| VAR === null) {}
console.log((false== 0) && (0== '') && (''== false)) // true
console.log(11== '11') // true
console.log(11=== '11') // false
^
[a, b] = [b, a];
// 异或运算,相同位取0,不同位取1,a ^ b ^ b = a, a ^ a ^ b = b
a = a ^ b
b = a ^ b
a = a ^ b
数值表示法
3e9
1e5; // 100000
2e-4; // 0.0002
-3e3; // -3000
小数点前的数字多于21位。 小数点后的零多于5个。
.5px
0x、0o和0b
二进制:只用0和1两个数字,前缀为0b,十进制13可表示为0b1101 八进制:只用0到7八个数字,前缀为0o、0,十进制13可表示为0o15、015 十六进制:只用0到9的十个数字,和a到f六个字母,前缀为0x,十进制13可表示为0xd
“话术”
Array.prototype.sort
返回值小于0,a排在b的左边 返回值等于0,a和b的位置不变 返回值大于0,a排在b的右边
[1,2,3,4].sort(() => .5- Math.random())
function shuffle(arrs) {
for(let i = arrs.length - 1; i > 0; i -= 1) {
const random = Math.floor(Math.random() * (i + 1));
[arrs[random], arrs[i]] = [arrs[i], arrs[random]];
}
}
Array.prototype.concat.apply
Array.prototype.concat.apply([], [1, [2,3], [4]])
function flattenDeep(arrs) {
let result = Array.prototype.concat.apply([], arrs);
while(result.some(item => item instanceofArray)) {
result = Array.prototype.concat.apply([], result);
}
return result;
}
Array.prototype.push.apply
let arrs = [1, 2, 3];
arrs = arrs.concat([4,5,6]);
const arrs = [1, 2, 3];
arrs.push.apply(arrs, [4, 5, 6]);
Array.prototype.length
const arrs = [];
arrs[5] = 1;
console.log(arrs.length); // 6
const arrs = [1, 2, 3];
delete arrs[2]; // 长度依然为3
const arrs = [1, 2, 3, 4];
arrs.length = 2; // arrs = [1, 2]
arrs.length = 0; // arrs = []
const arrs = [1, 2];
arrs.length = 5; // arrs = [1, 2,,,,]
const arrs = [1, 2, 3, 4];
arrs.length = 0; // arrs = []
let a = [1,2,3];
let b = [1,2,3];
let a1 = a;
let b1 = b;
a = [];
b.length = 0;
console.log(a, b, a1, b1); // [], [], [1, 2, 3], []
值需要为正整数 传递字符串会被尝试转为数字类型
Object.prototype.toString.call
19.1.3.6Object.prototype.toString ( )
When the toString method is called, the following steps are taken:
If the this value isundefined, return"[object Undefined]".
If the this value isnull, return"[object Null]".
Let O be ! ToObject(this value).
Let isArray be ? IsArray(O).
If isArray istrue, let builtinTag be "Array".
Elseif O is a String exotic object, let builtinTag be "String".
Elseif O has a [[ParameterMap]] internal slot, let builtinTag be "Arguments".
Elseif O has a [[Call]] internal method, let builtinTag be "Function".
Elseif O has an [[ErrorData]] internal slot, let builtinTag be "Error".
Elseif O has a [[BooleanData]] internal slot, let builtinTag be "Boolean".
Elseif O has a [[NumberData]] internal slot, let builtinTag be "Number".
Elseif O has a [[DateValue]] internal slot, let builtinTag be "Date".
Elseif O has a [[RegExpMatcher]] internal slot, let builtinTag be "RegExp".
Else, let builtinTag be "Object".
Let tag be ? Get(O, @@toStringTag).
IfType(tag) isnotString, set tag to builtinTag.
Return the string-concatenation of "[object ", tag, and"]".
Thisfunctionis the %ObjProto_toString% intrinsic object.
NOTE
Historically, thisfunction was occasionally used to access the String value of the [[Class]] internal slot that was used in previous editions of this specification as a nominal type tag for various built-in objects. The above definition of toString preserves compatibility for legacy code that uses toString as a test for those specific kinds of built-in objects. It does not provide a reliable type testing mechanism for other kinds of built-inor program defined objects. In addition, programs can use@@toStringTagin ways that will invalidate the reliability of such legacy type tests.
Object.create(null)
JSON.parse(JSON.stringify(Obj))
const obj = {a: 1};
obj.b = obj;
JSON.parse(JSON.stringify(obj)) // Uncaught TypeError: Converting circular structure to JSON
“理论”
Truthy与Falsy
if(false)
if(null)
if(undefined)
if(0)
if(NaN)
if('')
if("")
if(document.all)
console.log(NaN=== 0) // false
console.log(NaN=== NaN) // false
console.log(NaN== NaN) // false
Object.is(NaN, NaN) // true
Object.is(+0, -0) // false
原码, 反码, 补码
原码:00000000 00000000 00000000 00000101 反码:00000000 00000000 00000000 00000101 补码:00000000 00000000 00000000 00000101
原码:10000000 00000000 00000000 00000101 反码:11111111 11111111 11111111 11111010 补码:11111111 11111111 11111111 11111011
正数的原码、反码、补码都是它本身 负数的反码:在其原码的基础上, 符号位不变,其余各个位取反 负数的补码:负数的反码 + 1
1= 00000001
-1= 10000001
1[反码] + (-1)[反码] = 00000001+ 11111110= 11111111[反码] = 10000000[原码]
1[补码] + (-1)[补码] = 00000001+ 11111111= 00000000[原码]
参考资料
❤️ 看完两件事
如果你觉得这篇内容对你有所帮助,我想邀请你帮我两个小忙:点个「 在看
」,让更多的人也能看到这篇内容(喜欢不点在看,都是耍流氓 -_-)关注公众号「IT平头哥联盟」,一起进步,一起成长!
推荐阅读: