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的原型链中

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

  • 相关阅读:
    SpringCache使用
    SpringDataRedis使用
    Spring data jpa使用
    webpack的安装
    Vue基本使用
    Swagger使用
    gulp常用插件之gulp-notify使用
    gulp常用插件之gulp-beautify使用
    gulp常用插件之gulp-uglify使用
    gulp常用插件之gulp-size使用
  • 原文地址:https://www.cnblogs.com/pfcan66/p/3507099.html
Copyright © 2011-2022 走看看