Ternary operator, or the operation of the magical

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_36070288/article/details/100765460
var data2 = typeof data === 'function' ? getData(data) : data || {};

analysis:

  • Directly or operator-assignment will complain
var data2 = temp || 2;
console.log(data2); // 报错:Uncaught ReferenceError: temp is not defined at <anonymous>:1:13

Ternary operator in use:

  • data for the object
var data = {
  name: 'robert'
}
console.log(data2) // data || {} 得 {name: 'robert'}
  • data as a function of 
var data = function (obj) {
  return obj.name
}
console.log(data2) // getData(data)
  • data not declared
console.log(data2) // data || {} 得 {}

 

 

 

Guess you like

Origin blog.csdn.net/qq_36070288/article/details/100765460