0.前言
![一篇文章教你玩转Js类型转换](https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
- Object 与Primitive,需要Object转为Primitive
- String 与 Boolean,需要两个操作数同时转为Number。
- String/Boolean 与 Number,需要String/Boolean转为Number。
- undefined 与 null ,和所有其他值比较的结果都是false,他们之间==成立
- 如果参数是Date对象的实例,那么先toString()如果是原始值则返回,否则再valueOf(),如果是原始值则返回,否则报错。
- 如果参数不是Date对象的实例,同理,不过先valueOf再toString()。
1.一些例子
![] //false; +[] // 0 +![] // 0[]+[] // ""{}+{}//"[object Object][object Object]"{}+[]//0{a:0}+1 // 1[]+{}//"[object Object]"[]+![]//"false"{}+[]//0![]+[] // "false"''+{} //"[object Object]"{}+'' //0[]["map"]+[] //"function map() { }"[]["a"]+[] // "undefined"[][[]] + []// "undefined"+!![]+[] //"1"+!![] //11-{} //NaN1-[] //1true–1//0{}-1 //-1[]==![] //true2.从[]==![]开始
符号的优先度
2.1 []的反就是false?常见的一些转换:
2.2 等号两边对比
3.更多玩法3.1 间接获取数组方法
[]['push'](1) //[1][]["map"] //function map() { [native code] }[]["map"]+[] // "function map() { [native code] }"3.2 间接进行下标操作3.2.1数字的获取
3.2.2 字符串下标(![]+[])[+[]] //"f"(![]+[])[+!![]] // "a"
([][[]] + [])[(+!![] + [] + [] + +![] ) >> +!![]] +$$('*')[~~[]].nodeName[- ~ -+~[]] +(this + [])[+!![]+[] + +[] – !!{} – !!{}] +([]+![])[+!!{} << +!![] -~[]] +({}+{})[+!![] -~[]]4. 两个面试题
4.1 (a==1 && a==2 && a==3) 能不能为true
var a = { num: 0, valueOf: function() { return this.num += 1 }};var eq = (a==1 && a==2 && a==3);console.log(eq);//或者改写他的tostring方法 var num = 0;Function.prototype.toString = function(){ return ++num;}function a(){}//还可以改写ES6的symbol类型的toP的方法var a = {[Symbol.toPrimitive]: (function (i) { return function(){return ++i } }) (0)};
var a = { num: 4, valueOf: function() { return this.num -= 1 }};var res = (a==3 && a==2 && a==1);console.log(res);
var b = 1Object.defineProperty(window, 'a', { get:function() { return b++; }})var s = (a===1 && a===2 && a === 3 )console.log(s)
4.2 完善Cash打印三个101
class Cash {}const a = new Cash(1)const b = new Cash(100)console.log(`${a.add(b)},${Cash.add(a,b)},${new Cash(a+b)}`) // 101,101,101
class Cash { constructor (a) { this.m = a // 缓存真正的值 this.valueOf = function () { console.log('value') return a } } add ($) { // a.add(b) return this.m + $ } toString () { // 隐式转换调用 return this.m } static add (v1, v2) { //Cash.add return v1 + v2 }}END
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/253364.html