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

相关文章
相关标签/搜索