zoukankan      html  css  js  c++  java
  • javascript--- hasOwnProperty、instanceof 、typeof的区别

      typeof

        作用:用来判断变量的类型  

        返回值: string  只有以下几种:number、boolean、string、object、undefined、function[很容易漏掉这个]

        形式: typeof  a

      instanceof

        作用:用来判断变量是否是函数或者是对象(instanceof 有继承的意思,继承于Object、Array、Function)  instanceof只能用来判断对象和函数,不能用来判断字符串和数字(会返回false)等 ----(为什么不能呢?)

        返回值:boolean

        形式: a instanceof  Object  /  b instanceof  Array

      constructor

        当我们使用js系统或者自己创建的对象的时候,会默认的加上的属性

        比如:

    var arr = [1,2,3];  //创建一个数组对象
    arr.prototype.constructor = Array;  //这一句是系统默认加上的

        所以我们就可以这样来判断:

    var arr = [1,2,3,1]; 
    alert(arr.constructor === Array);   // true

      hasOwnProperty

        hasOwnProperty是一个函数, 该函数用于指示一个对象自身(不包括原型链)是否具有指定名称的属性。如果有,返回true,否则返回false。该方法属于Object对象,由于所有的对象都"继承"了Object的对象实例,因此几乎所有的实例对象都可以使用该方法。

  • 相关阅读:
    Luogu2751 [USACO Training4.2]工序安排Job Processing
    BZOJ4653: [Noi2016]区间
    BZOJ1537: [POI2005]Aut- The Bus
    CF1041F Ray in the tube
    POJ1186 方程的解数
    Luogu2578 [ZJOI2005]九数码游戏
    BZOJ2216: [Poi2011]Lightning Conductor
    CF865D Buy Low Sell High
    BZOJ1577: [Usaco2009 Feb]庙会捷运Fair Shuttle
    几类区间覆盖
  • 原文地址:https://www.cnblogs.com/first-time/p/7010922.html
Copyright © 2011-2022 走看看