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

  

 
love life,love work!---身为屌丝,向往的仍然是逆袭的故事。

hasOwnProperty()&&isPrototypeOf()的更多相关文章

  1. isPrototypeOf,hasOwnProperty

    在看jquery源码的过程中,了解到isPrototypeOf属性.此属性只是Object.prototype的自有属性,即: Object.prototype.hasOwnProperty('isP ...

  2. extend

    这段时间在写一个预览图片的插件, 被我老大说了无数次了,不多说啥,说多了都是泪 昨天看着我的代码他说你用了extend,那你知道是什么意思吗 我只知道是扩展的意思,瞬间觉得自己弱爆了 真的 然后今天看 ...

  3. Javascript中prototype属性详解

    在典型的面向对象的语言中,如java,都存在类(class)的概念,类就是对象的模板,对象就是类的实例.但是在Javascript语言体系中,是不存在类(Class)的概念的,javascript中不 ...

  4. jQuery静态方法isPlainObject,isEmptyObject方法使用和源码分析

    isPlainObject方法 测试对象是否是纯粹的对象(通过 "{}" 或者 "new Object" 创建的) 示例: //测试是否为纯粹的对象 jQuer ...

  5. javascript中的对象,原型,原型链和面向对象

    一.javascript中的属性.方法 1.首先,关于javascript中的函数/“方法”,说明两点: 1)如果访问的对象属性是一个函数,有些开发者容易认为该函数属于这个对象,因此把“属性访问”叫做 ...

  6. Object.prototype 与 Function.prototype 与 instanceof 运算符

    方法: hasOwnProperty isPrototypeOf propertyIsEnumerable hasOwnProperty 该方法用来判断一个对象中的某一个属性是否是自己提供的( 住要用 ...

  7. JavaScript中in操作符(for..in)、Object.keys()和Object.getOwnPropertyNames()的区别

    ECMAScript将对象的属性分为两种:数据属性和访问器属性.每一种属性内部都有一些特性,这里我们只关注对象属性的[[Enumerable]]特征,它表示是否通过 for-in 循环返回属性,也可以 ...

  8. Javascript中prototype属性的详解

    原文链接:http://www.cnblogs.com/Uncle-Keith/p/5834289.html 在典型的面向对象的语言中,如java,都存在类(class)的概念,类就是对象的模板,对象 ...

  9. js1常用的东西

    1 .ready 与resize方法.$(inject).ready(function() { var windowWidth = $(document.body).outerWidth(true); ...

随机推荐

  1. 自动获取socket链接状态

    C# TcpClient在连接成功后无法检测连接状态,即使对方关闭了网络连接.以下扩展可检测连接状态: static class TcpClientEx { public static bool Is ...

  2. Android permission

    1. users-permission Users-permission is the permission that this app should acquire, so that the app ...

  3. umf(转)

    深入浅出Eclipse Modeling Framework (EMF) Eclipse Modeling Framework (EMF),简单的说,就是Eclipse提供的一套建模框架,可以用EMF ...

  4. LeetCode OJ 147. Insertion Sort List

    Sort a linked list using insertion sort. Subscribe to see which companies asked this question 解答 对于链 ...

  5. flex 正则 验证

    验证数字:^[-]*$ 验证n位的数字:^\d{n}$ 验证至少n位数字:^\d{n,}$ 验证m-n位的数字:^\d{m,n}$ 验证零和非零开头的数字:^(|[-][-]*)$ 验证有两位小数的正 ...

  6. delphi日期格式化免操作系统依赖单元

    delphi免操作系统依赖单元 经常在windows里面,我们需要yyyy-mm-dd格式日期,可是用户却没有设置成这个 格式,而用程序去修改用户的日期格式设置,是不明智的,将影响其他程序的运行. 本 ...

  7. Mac上的抓包工具Charles

    http://blog.csdn.net/jiangwei0910410003/article/details/41620363 $********************************** ...

  8. 。net 添加或获取文件关联

    文件关联设置 2011-02-07 14:25:36|  分类: VB.net2008或2010 |  标签:文件关联  |举报|字号 订阅     原理:以后缀名为.txt为例 方式一: 1.在注册 ...

  9. [转载]tail No space left on device

    转载http://www.chenxie.org/?p=717 # tail -f ../logs/catalina.outtail: cannot watch `../logs/catalina.o ...

  10. mysql远程连接:ERROR 1130 (HY000): Host '*.*.*.*' is not allowed to connect to this MySQL server解决办法

    安装完MySQL后,远程连接数据库的时候,出现 ERROR 1130 (HY000): Host '192.168.0.1' is not allowed to connect to this MyS ...