zoukankan      html  css  js  c++  java
  • javascript 中 in操作符

    in in 操作检查对象中是否有名为 property 的属性。也可以检查对象的原型,以便知道该属性是否为原型链的一部分。

      对于一般的对象属性需要用字符串指定属性的名称

    var mycar = {make: "Honda", model: "Accord", year: 1998};
    "make" in mycar  // returns true
    "model" in mycar // returns true
    

      对于数组属性需要指定数字形式的索引值来表示数组的属性名称(固有属性除外,如length

    // Arrays
    var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
    0 in trees        // returns true
    3 in trees        // returns true
    6 in trees        // returns false
    "bay" in trees    // returns false (you must specify the index number,
                      // not the value at that index)
    "length" in trees // returns true (length is an Array property)
    

       in的右边必须是一个对象,如:你可以指定一个用String构造器生成的,但是不能指定字符串直接量的形式:

    var color1 = new String("green");
    "length" in color1 // returns true
    var color2 = "coral";
    "length" in color2 // generates an error (color is not a String object)
    

      如果你使用delete操作符删除了一个属性,再次用in检查时,会返回false,如:

    var mycar = {make: "Honda", model: "Accord", year: 1998};
    delete mycar.make;
    "make" in mycar;  // returns false
     
    var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
    delete trees[3];
    3 in trees; // returns false
    

      

  • 相关阅读:
    对我影响最大的老师
    介绍自己
    JavaScript 时间特效 显示当前时间
    js 获取函数的所有参数名
    node.js 在函数内获取当前函数
    js 实现二叉排序树
    命令行下mysql的部分操作
    浅析js的函数的按值传递参数
    返回上一页时,保存恢复浏览记录(模拟返回不刷新)
    让mongodb执行js文件
  • 原文地址:https://www.cnblogs.com/teamobaby/p/3733275.html
Copyright © 2011-2022 走看看