zoukankan      html  css  js  c++  java
  • 比较typeof与instanceof

    相同点:JavaScript 中 typeof 和 instanceof 常用来判断一个变量是否为空,或者是什么类型的。

    typeof的定义和用法:返回值是一个字符串,用来说明变量的数据类型。

    细节:

    (1)、typeof 一般只能返回如下几个结果:number,boolean,string,function,object,undefined。

    (2)、typeof 来获取一个变量是否存在,如 if(typeof a!="undefined"){alert("ok")},而不要去使用 if(a) 因为如果 a 不存在(未声明)则会出错。

    (3)、对于 Array,Null 等特殊对象使用 typeof 一律返回 object,这正是 typeof 的局限性。

    Instanceof定义和用法:instanceof 用于判断一个变量是否属于某个对象的实例。

    实例演示:

    a instanceof b?alert("true"):alert("false"); //a是b的实例?真:假

    var a = new Array(); 
    alert(a instanceof Array);  // true
    alert(a instanceof Object)  // true
    

    如上,会返回 true,同时 alert(a instanceof Object) 也会返回 true;这是因为 Array 是 object 的子类。

    function test(){};
    var a = new test();
    alert(a instanceof test)   // true

    细节:

    (1)、如下,得到的结果为‘N’,这里的 instanceof 测试的 object 是指 js 语法中的 object,不是指 dom 模型对象。

    if (window instanceof Object){ alert('Y')} else {  alert('N');}  // 'N'
  • 相关阅读:
    今天面试一些程序员(新,老)手的体会
    UVA 10635 Prince and Princess
    poj 2240 Arbitrage
    poj 2253 Frogger
    poj 2485 Highways
    UVA 11258 String Partition
    UVA 11151 Longest Palindrome
    poj 1125 Stockbroker Grapevine
    poj 1789 Truck History
    poj 3259 Wormholes
  • 原文地址:https://www.cnblogs.com/5jingjing/p/8539166.html
Copyright © 2011-2022 走看看