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. const实现

    [const实现]

  2. Linux 夸平台 移植 Win32

    1.代码格式 大量的 警告 不识别的字符(936),请保存为unicode 以免丢失数据,好多参考说忽略此警告. 但是很多错误都是由于这个警告引起的.将大量的.h .cpp 的utf 8 数据用txt ...

  3. html() 和 text() 方法的区别

    html()方法仅支持XHTML的文档,不能用于XML文档,而text()既支持HTML文档,也 支持XML文档. html():没有参数,用于获取html的值: html(val):有参数,用于设置 ...

  4. lucene query

    在lucene的搜索中,最重要的无疑就是对query的理解和掌握了.这里扒拉一下源码(版本3.5.0)的query和query实现: query是一个抽象类,实现类有以下几个: termQuery m ...

  5. JAVA学习<四>

    一. 数组: Java 中操作数组只需要四个步骤: 1. 声明数组 语法:  数据类型[ ] 数组名: 或者   数据类型 数组名[ ]: 其中,数组名可以是任意合法的变量名. 2. 分配空间 简单地 ...

  6. android笔记:DatePickerDialog日期设置对话框

    在开发中,可以通过DatePickerDialog来设置日期,TimePickerDialog来设置时间. 实例化DatePickerDialog对象之后,再调用show方法就可以显示对话框了. 具体 ...

  7. Ubuntu遇到Please ensure that adb is correctly located at '...adb.exe' and can be executed 问题解决方法

    上次我们在SDK更新的到最新的Android L版本之后,我发现我的ADT和android指定的版本不对应,我的ADT是22版本的,android L需要23版本以上的,版本不对应的话就无法加载这个S ...

  8. Git常用命令(自己总是忘记,整理在这里)

    1.git init    初始化一个空的git仓库 2.git clone +SSH地址    clone新的项目到本地 3.git add     git add file 4.git commi ...

  9. php中英文截取无乱码 包括全角下的字符

    符合UTF-8下,如果GBK下  改为  $content .= $str[$sing].$str[$sing+1];        $sing += 3; 改为 $sing += 2; /**    ...

  10. Java并发(8):CountDownLatch、CyclicBarrier、Semaphore、Callable、Future

    CountDownLatch.CyclicBarrier.Semaphore.Callable.Future  都位于java.util.concurrent包下,其中CountDownLatch.C ...