zoukankan      html  css  js  c++  java
  • javascript的坑

    1 for in循环:使用它时,要主要遍历的是所有可枚举的属性(实例以及原型中的属性)

    function Person(name){
        this.name = name;
    }
    Person.prototype.getName=function(){
        return this.name;
    }
    var p = new Person('mengxb');
    for(prop in p) {
    //此时会遍历到原型上的getName,如果不想遍历原型上的属性和方法,可以用hasOwnProperty()
    if(p.hasOwnProperty(prop)){
    // some opertion here
    } }

    另外还有一个in操作符,可以用来判断某个属性是否能被某个实例访问到

    //利用上面的代码
    alert(name in p);//true
    alert(getName in p);//true

    小技巧:可以利用in操作符以及hasOwnProperty方法来写一个新方法hasPrototypeProperty()

    function hasPrototypeProperty(object, prop){
      return (prop in  object) && !object.hastOwnProperty(prop);
    }
  • 相关阅读:
    python解析网页
    node.js 爬虫
    c++ split实现
    foldl foldr
    爬虫http header gzip
    命令[10]
    命令[08]
    命令[15]
    命令[13]
    命令[11]
  • 原文地址:https://www.cnblogs.com/mengxiang-1234/p/4684952.html
Copyright © 2011-2022 走看看