in 操作符会在通过对象能够访问给定属性时返回true,无论该属性存在于实例中还是原型中

2017-06-21 11:15:00
admin
原创
256
<!DOCTYPE html>
<html>
<head>
    <title>Prototype Pattern Example 4</title>
    <script type="text/javascript">
            
        function Person(){
        }
        
        Person.prototype.name = "Nicholas";
        Person.prototype.age = 29;
        Person.prototype.job = "Software Engineer";
        Person.prototype.sayName = function(){
            alert(this.name);
        };
        
        var person1 = new Person();
        var person2 = new Person();
        
        alert(person1.hasOwnProperty("name"));  //false
        alert("name" in person1);  //true
        
        person1.name = "Greg";
        alert(person1.name);   //"Greg" from instance
        alert(person1.hasOwnProperty("name"));  //true
        alert("name" in person1);  //true
        
        alert(person2.name);   //"Nicholas" from prototype
        alert(person2.hasOwnProperty("name"));  //false
        alert("name" in person2);  //true
        
        delete person1.name;
        alert(person1.name);   //"Nicholas" - from the prototype
        alert(person1.hasOwnProperty("name"));  //false
        alert("name" in person1);  //true

    </script>
</head>
<body>

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