zoukankan      html  css  js  c++  java
  • instanceof关键字

    <html>
    <head>
    
    <script type="text/javascript">
         //instanceof
    	 var x="23";  //String str="abc" String str= new String("ac");
    	 var y = new String("23");
    	 alert(typeof (y+"233"));
         alert(y instanceof Object);  //
    	 alert(x instanceof Object);  //
    </script>
    </head>
    <body>
    
    </body>  
    </html>
    

    在x="23"这个x表示的是一个字符串的类型;

    y=new String("23")中y表示的是一个对象--->在我的理解相当于对象的引用(java);

    一些copy

     from:

    http://blog.sina.com.cn/s/blog_532751d90100iv1r.html

    JavaScript 中 typeof 和 instanceof 常用来判断一个变量是否为空,或者是什么类型的。但它们之间还是有区别的:

    typeof

    typeof 是一个一元运算,放在一个运算数之前,运算数可以是任意类型。

    它返回值是一个字符串,该字符串说明运算数的类型。typeof 一般只能返回如下几个结果:

    number,boolean,string,function,object,undefined。我们可以使用 typeof 来获取一个变量是否存在,如 if(typeof a!="undefined"){alert("ok")},而不要去使用 if(a) 因为如果 a 不存在(未声明)则会出错,对于 Array,Null 等特殊对象使用 typeof 一律返回 object,这正是 typeof 的局限性。

    网上的一个小例子:

    instanceof

    instance:实例,例子

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

    instanceof 用于判断一个变量是否某个对象的实例,如 var a=new Array();alert(a instanceof Array); 会返回 true,同时 alert(a instanceof Object) 也会返回 true;这是因为 Array 是 object 的子类。再如:function test(){};var a=new test();alert(a instanceof test) 会返回

    谈到 instanceof 我们要多插入一个问题,就是 function 的 arguments,我们大家也许都认为 arguments 是一个 Array,但如果使用 instaceof 去测试会发现 arguments 不是一个 Array 对象,尽管看起来很像。

    另外:

    测试 var a=new Array();if (a instanceof Object) alert('Y');else alert('N');

    得'Y’

    但 if (window instanceof Object) alert('Y');else alert('N');

    得'N'

    所以,这里的 instanceof 测试的 object 是指 js 语法中的 object,不是指 dom 模型对象。

    使用 typeof 会有些区别

    alert(typeof(window)) 会得 object

  • 相关阅读:
    win10 UWP button
    内网分享资源
    内网分享资源
    CF724F Uniformly Branched Trees
    win10 UWP FlipView
    win10 UWP FlipView
    win10 UWP FlipView
    搭建阿里云 centos mysql tomcat jdk
    搭建阿里云 centos mysql tomcat jdk
    win10 UWP 申请微软开发者
  • 原文地址:https://www.cnblogs.com/lonecloud/p/5487950.html
Copyright © 2011-2022 走看看