js
變量是否存在: js 檢測變量是否存在 Js中判斷變量存不存在的問題
數組是否爲空: js判斷數組是否爲空
對象是否爲空:js中判斷對象是否爲空
特別的 javascript判斷一個對象或對象屬性是否存在:
檢測對象的屬性是否存在不必用typeof:
var obj = {};
if(!obj.b){
console.log(“not have attribute ‘b’”); //not have attribute ‘b’
}
// 直接用未定義的對象來判斷,會出錯。如下例。
if(!myObj1){
myObj1 = {}; //ReferenceError: myObj is not defined
}
// 需要如下初始化,重點在“var”。
if (!myObj2) {
console.log(“aaa”); //aaa
var myObj2 = { };
}
//使用hasOwnProperty方法
if(!this.hasOwnProperty(‘myObj8’)){
console.log(“8”);
this.myObj8 = {};
}
// vue
v-if=" arr!= undefined && arr.includes(1)"
v-model=" typeof(temp.detail.fifteen) != 'undefined' ? temp.detail.fifteen : 0 "
總結
php中isset($abc) 等價於js中 typeof(abc)!=“undefined”PHP中的empty($abc) 等價於js 中 abc==null