hasOwnProperty()只在属性存在于实例中时才返回true

2017-06-21 11:16:00
admin
原创
364
<!DOCTYPE html>
<html>
<head>
    <title>Prototype Pattern Example</title>
    <script type="text/javascript">
            
        function hasPrototypeProperty(object, name){
            return !object.hasOwnProperty(name) && (name in object);
        }
            
        function Person(){
        }
        
        Person.prototype.name = "Nicholas";
        Person.prototype.age = 29;
        Person.prototype.job = "Software Engineer";
        Person.prototype.sayName = function(){
            alert(this.name);
        };
        
        var person = new Person();        
        alert(hasPrototypeProperty(person, "name"));  //true
        
        person.name = "Greg";
        alert(hasPrototypeProperty(person, "name"));  //false        

    </script>
</head>
<body>

</body>
</html>
文章分类
联系我们
电话: 18902328227
Email: 2689701946@qq.com
QQ: 2689701946
微信: 18902328227
地址: 广州番禺区繁华路
  • 在线客服
  • 关注微信
  • 联系电话
    • 欢迎咨询
    • 欢迎加入QQ群 303257430
  • 公众号:源宝网络
    • 联系电话及微信号 18902328227