soonfy / issue-blog

blog using issue
0 stars 0 forks source link

javascript tricks #58

Open soonfy opened 5 years ago

soonfy commented 5 years ago

函数内部的连续赋值操作

function init () {
  let numA = numB = 5
}
init()
console.log(typeof numA)
console.log(numB)
// undefined
// 5

soonfy

soonfy commented 5 years ago

对象的连续赋值操作

let objA = {name: 'xiaobo'}
let objB = objA
objA.obj = objA = {name: 'xiaoming'}
console.log(objA)
console.log(objB)
console.log(objA.obj)
// { name: 'xiaoming' }
// { name: 'xiaobo', obj: { name: 'xiaoming' } }
// undefined

soonfy

soonfy commented 5 years ago

加号 + 操作符

[] + {}
{} + []
[object Object]
0

soonfy

soonfy commented 5 years ago

<=, >= 操作符

a = {age: 18}
b = {age: 18}

// questions
a > b
a < b
a == b
a <= b
a >= b
false
false
false
true
true

soonfy

soonfy commented 5 years ago

类型转换 String() and +

let a = {
  valueOf(){return 123},
  toString(){return 321}
}
let b = String(a)
let c = a + ''
b = '321'
c = '123'

soonfy

soonfy commented 5 years ago

&& and ||

let a = false && false || true
let b = true || false && false
a = true
b = true

soonfy