zoukankan      html  css  js  c++  java
  • hasOwnProperty()&&isPrototypeOf()

    1、hasOwnProperty()

    hasOwnProperty()函数用于指示一个对象自身(不包括原型链)是否具有指定名称的属性。如果有,返回true,否则返回false

    该方法属于Object对象,由于所有的对象都"继承"了Object的对象实例,因此几乎所有的实例对象都可以使用该方法。

    IE 5.5+、FireFox、Chrome、Safari、Opera等主流浏览器均支持该函数。

    语法:

    object.hasOwnProperty(propertyName)

    参数:

    参数描述
    propertyName String类型指定的属性名称



    返回值:

    hasOwnProperty()函数的返回值为Boolean类型。如果对象object具有名称为propertyName的属性,则返回true,否则返回false

    此方法不会检查对象的原型链中是否存在该属性,该属性只有是对象本身的一个成员才会返回true

     1 function Site(){
     2     this.name = "CodePlayer";
     3     this.url = "http://www.365mini.com/";
     4 
     5     this.sayHello = function(){
     6         document.writeln("欢迎来到" + this.name);
     7     };
     8 }
     9 
    10 var obj = {
    11     engine: "PHP"
    12     ,sayHi: function(){
    13         document.writeln("欢迎访问" + this.url);
    14     }
    15 };
    16 // 使用对象obj覆盖Site本身的prototype属性
    17 Site.prototype = obj;
    18 
    19 var s =  new Site();
    20 document.writeln( s.hasOwnProperty("name") ); // true
    21 document.writeln( s.hasOwnProperty("sayHello") ); // true
    22 // 以下属性继承自原型链,因此为false
    23 document.writeln( s.hasOwnProperty("engine") ); // false
    24 document.writeln( s.hasOwnProperty("sayHi") ); // false
    25 document.writeln( s.hasOwnProperty("toString") ); // false
    26 
    27 // 想要查看对象(包括原型链)是否具备指定的属性,可以使用in操作符
    28 document.writeln( "engine" in s ); // true
    29 document.writeln( "sayHi" in s ); // true
    30 document.writeln( "toString" in s ); // true

    2、isPrototypeOf()

    isPrototypeOf()函数用于指示对象是否存在于另一个对象的原型链中。如果存在,返回true,否则返回false

    该方法属于Object对象,由于所有的对象都"继承"了Object的对象实例,因此几乎所有的实例对象都可以使用该方法。

    IE 5.5+、FireFox、Chrome、Safari、Opera等主流浏览器均支持该函数。

    语法:

    prototypeObject.isPrototypeOf( object )

    参数:

    参数描述
    object Object类型一个对象,将对其原型链进行检查



    返回值:

    isPrototypeOf()函数的返回值为Boolean类型。如果object当前的原型链中存在prototypeObject对象,则isPrototypeOf()方法返回true。原型链用于在同一个对象类型的不同实例之间共享功能。如果object不是对象,或者prototypeObject对象不出现在object的原型链中,则该方法返回false

    function Site(){
        this.name = "CodePlayer";
        this.url = "http://www.365mini.com/";
    
        this.sayHello = function(){
            document.writeln("欢迎来到" + this.name);
        };
    }
    
    var s =  new Site();
    document.writeln( Site.prototype.isPrototypeOf(s) ); // true
    
    var obj = {
        engine: "PHP"
        ,sayHi: function(){
            document.writeln("欢迎访问" + this.url);
        }
    };
    // 使用对象obj覆盖Site本身的prototype属性
    Site.prototype = obj;
    
    var s2 =  new Site();
    document.writeln( obj.isPrototypeOf(s2) ); // true
    

      公共示例:

    function siteAdmin(nickName, siteName) {
                this.nickName = nickName;
                this.siteName = siteName;
            }
    
            siteAdmin.prototype.showAdmin = function () {
                alert(this.nickName + "是" + this.siteName + "的站长!");
            };
    
            siteAdmin.prototype.showSite = function (siteUrl) {
                this.siteUrl = siteUrl;
                return this.siteName + "的地址是" + this.siteUrl;
            };
            var matou = new siteAdmin("愚人码头", "WEB前端开发");
            var matou2 = new siteAdmin("愚人码头", "WEB前端开发");
            matou.age = "30";
    
            alert(matou.hasOwnProperty("nickName"));//true
            alert(matou.hasOwnProperty("age"));//true
            alert(matou.hasOwnProperty("showAdmin"));//false
            alert(matou.hasOwnProperty("siteUrl"));//false
            alert(siteAdmin.prototype.hasOwnProperty("showAdmin"));//true
            alert(siteAdmin.prototype.hasOwnProperty("siteUrl"));//false
            alert(siteAdmin.prototype.isPrototypeOf(matou));//true
            alert(siteAdmin.prototype.isPrototypeOf(matou2));//true
    

      

     
  • 相关阅读:
    Maven下Flex国际化配置
    Adobe AIR and Flex
    jQuery: 刨根问底 attr and prop两个函数的区别
    HTML5[8]: 图文混排,图片与文字居中对齐
    HTML5[7]: 实现网页版的加载更多
    HTML5[6]:多行文本显示省略号
    HTML5[5]:在移动端禁用长按选中文本功能
    HTML5[4]:去除不必要的标签,完全使用css实现样式
    HTML5[3]:中文换行
    HTML5[2]:使用viewport控制手机浏览器布局
  • 原文地址:https://www.cnblogs.com/liushenghuan/p/5752568.html
Copyright © 2011-2022 走看看