码迷,mamicode.com
首页 > 其他好文 > 详细

hasOwnProperty()&&isPrototypeOf()

时间:2016-08-14 10:13:51      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

  公共示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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

  

 

hasOwnProperty()&&isPrototypeOf()

标签:

原文地址:http://www.cnblogs.com/libin-1/p/5769563.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!