zoukankan      html  css  js  c++  java
  • 深入解析hasOwnProperty与isPrototypeOf

    这里采用一个实例来说明:

     1 function Person(name) {
     2             //以下都是Person的OwnProperty
     3             this.name = name;
     4             this.showMe = function () {
     5                 alert(this.name);
     6             };
     7         }
     8         //每一个'类'都有prototype属性,而这里的protype指向的是一个prototype对象,所以这里的prototype不是OwnProperty
     9         Person.prototype.from = function () {
    10             alert("I come from prototype");
    11         };
    12         var father = new Person('js');
    13 
    14         /*object.hasOwnProperty(proName); 
    15         判断proName的名称是不是object对象的一个属性或对象*/
    16         alert(father.hasOwnProperty("name"));//true
    17         alert(father.hasOwnProperty("from"));//false
    18         alert(Person.prototype.hasOwnProperty("name"));//false
    19         alert(Person.prototype.hasOwnProperty("from"));//true
    20 
    21         /*object1.isPrototypeOf(object2);
    22         对象object1是否存在于另一个对象object2的原型链中*/
    23         alert(Person.prototype.isPrototypeOf(father));//true
    24         //【因为Person.prototype只有constructor,from,但father里有name,showMe之外还有constructor,from】
    View Code

    如如图所示:Person.prototype在father的原型链中

    是不是很容易就理解了呢!~_~

  • 相关阅读:
    构建之法 阅读笔记01
    个人作业1 -数组
    进度一
    开课博客
    生活尝试
    人月神话3
    安卓开发工具
    人月神话 2
    Qt 的入门小程序
    提问的智慧 摘抄(How To Ask Questions The Smart Way)
  • 原文地址:https://www.cnblogs.com/pfcan66/p/3507099.html
Copyright © 2011-2022 走看看