zoukankan      html  css  js  c++  java
  • js属性对象的hasOwnProperty方法

    判断自生属性与继承性

     function foo() {
                this.name = 'foo'
                this.sayHi = function () {
                    console.log('Say Hi')
                }
            }
    
            foo.prototype.sayGoodBy = function () {
                console.log('Say Good By')
            }
    
            var myPro = new foo()
    
            console.log(myPro.name) // foo
            console.log(myPro.hasOwnProperty('name')) // true
            console.log(myPro.hasOwnProperty('toString')) // false
            console.log(myPro.hasOwnProperty('hasOwnProperty')) // fasle
            console.log(myPro.hasOwnProperty('sayHi')) // true
            console.log(myPro.hasOwnProperty('sayGoodBy')) // false
            console.log('sayGoodBy' in myPro) // true

    判断自生属性是否存在

    var o = new Object();
    o.prop = 'exists';
    function changeO() {
        o.newprop = o.prop;
        delete o.prop;
    }
    console.log(o.hasOwnProperty('prop'));  // true
    changeO();
    console.log(o.hasOwnProperty('prop'));  // false

    遍历一个对象的所有自身属性

    在看开源项目的过程中,经常会看到类似如下的源码。for...in循环对象的所有枚举属性,然后再使用hasOwnProperty()方法来忽略继承属性。

    var buz = {
        fog: 'stack'
    };
    
    for (var name in buz) {
        if (buz.hasOwnProperty(name)) {
            alert("this is fog (" + name + ") for sure. Value: " + buz[name]);
        }
        else {
            alert(name); // toString or something else
        }
    }
      
    
    
  • 相关阅读:
    缓存
    mybatis(ibatis)理解
    hibernate理解
    linux vi 命令
    Activity中ConfigChanges属性的用法
    sendStickyBroadcast和sendStickyOrderedBroadcast
    广播 BroadCastReceiver
    Android四大基本组件介绍与生命周期
    ubuntu14.04 开机自动运行应用程序
    Android中BindService方式使用的理解
  • 原文地址:https://www.cnblogs.com/punisher999/p/12251311.html
Copyright © 2011-2022 走看看