JavaScript 檢驗變量

建立: 2019/02/20html

遷入: 刪除【WIP】標籤(由於隨時更新, 不存在完成不完成)函數

    從【JavaScript 式與運算符】遷入typeofspa

更新: 2019/03/25 補充靜態變量與參照變量prototype

更新: 2019/06/06 增長==便於找到false判斷, 增長false也是code

 

parseInt, parseFloat, isNaN, Object.getPrototypeOf,htm

 靜態變量與參照變量

 

 靜態變量  數值,字符串, true/false, undefined/null, Symbol
 參照變量  除了靜態變量外的一切量

 

 類相關
 判斷類型
 typeof
 返回對象值數據類型,字符串 
typeof "A" //string
 
 數據  返回值 
 數字和NaN  "number"
 字符串  "string"
 未定義值
 (就一個undefined)
 "undefined"
 空值null  "object"
 符號
 Symbol("sample")
 "symbol"
 函數之外的對象  "object"
 函數  "function"
   
   
 instanceof  
a instanceof A // 對象 instanceof 構造函數

function F() {};
var obj = new F();
console.log(obj instanceof F); // true
console.log(obj instanceof Object); // true
console.log(obj instanceof Date); // false

 

 prototype.isPrototypeOf()  
prototype.isPrototypeOf(對象);

function F() {};
var obj = new F();
console.log(F.prototype.isPrototypeOf(obj)); // true
console.log(Object.prototype.isPrototypeOf(obj)); // true
console.log(Date.prototype.isPrototypeOf(obj)); // false

 

 

 屬性相關

 判斷指定屬性是否存在對象

 in

 注: 包括繼承的(__proto__的)blog

 

  var a = { a: 1 };
  var b = Object.create(a, {
    b: {
      value: 1,
      writbale: true,
      enumerable: true,
      configurable: true
    }
  });
  console.log(b);
  console.log('------------------in--------------------');
  console.log('b' in b); // true
  console.log('a' in b); // true
  console.log('--------------hasOwnProperty---------------');
  console.log(b.hasOwnProperty('b')); // true
  console.log(b.hasOwnProperty('a')); // false

 

 Object.prototype.hasOwnProperty(key)

 注: 僅驗證當前類(當前的prototype)繼承

 

  var a = { a: 1 };
  var b = Object.create(a, {
    b: {
      value: 1,
      writbale: true,
      enumerable: true,
      configurable: true
    }
  });
  console.log(b);
  console.log('------------------in--------------------');
  console.log('b' in b); // true
  console.log('a' in b); // true
  console.log('--------------hasOwnProperty---------------');
  console.log(b.hasOwnProperty('b')); // true
  console.log(b.hasOwnProperty('a')); // false

 

 

 

 Object.is(value1, value2)  判斷value1和value2是否相同
 Object.isExtensible(obj)  判斷是否能夠擴展
 Object.isFrozen(obj)  判斷是否被凍結
 Object.isSealed(obj)  判斷是否受保護 
 運算符

 

 與運算符  
a && b

 若a可轉換爲true,則返回b;不然,返回aip

 或運算符
a || b

 若a可轉換爲true, 返回a; 不然返回b

 非運算符  
!a

若a可轉換爲true, 返回false; 不然返回true

   
   

 

 會被轉換爲false的表達式

==

 ● null

 ● NaN

 ● 0

 ● "", '', ``

 ● undefined

 ● false

相關文章
相關標籤/搜索