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
。
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()的更多相关文章
- isPrototypeOf,hasOwnProperty
在看jquery源码的过程中,了解到isPrototypeOf属性.此属性只是Object.prototype的自有属性,即: Object.prototype.hasOwnProperty('isP ...
- extend
这段时间在写一个预览图片的插件, 被我老大说了无数次了,不多说啥,说多了都是泪 昨天看着我的代码他说你用了extend,那你知道是什么意思吗 我只知道是扩展的意思,瞬间觉得自己弱爆了 真的 然后今天看 ...
- Javascript中prototype属性详解
在典型的面向对象的语言中,如java,都存在类(class)的概念,类就是对象的模板,对象就是类的实例.但是在Javascript语言体系中,是不存在类(Class)的概念的,javascript中不 ...
- jQuery静态方法isPlainObject,isEmptyObject方法使用和源码分析
isPlainObject方法 测试对象是否是纯粹的对象(通过 "{}" 或者 "new Object" 创建的) 示例: //测试是否为纯粹的对象 jQuer ...
- javascript中的对象,原型,原型链和面向对象
一.javascript中的属性.方法 1.首先,关于javascript中的函数/“方法”,说明两点: 1)如果访问的对象属性是一个函数,有些开发者容易认为该函数属于这个对象,因此把“属性访问”叫做 ...
- Object.prototype 与 Function.prototype 与 instanceof 运算符
方法: hasOwnProperty isPrototypeOf propertyIsEnumerable hasOwnProperty 该方法用来判断一个对象中的某一个属性是否是自己提供的( 住要用 ...
- JavaScript中in操作符(for..in)、Object.keys()和Object.getOwnPropertyNames()的区别
ECMAScript将对象的属性分为两种:数据属性和访问器属性.每一种属性内部都有一些特性,这里我们只关注对象属性的[[Enumerable]]特征,它表示是否通过 for-in 循环返回属性,也可以 ...
- Javascript中prototype属性的详解
原文链接:http://www.cnblogs.com/Uncle-Keith/p/5834289.html 在典型的面向对象的语言中,如java,都存在类(class)的概念,类就是对象的模板,对象 ...
- js1常用的东西
1 .ready 与resize方法.$(inject).ready(function() { var windowWidth = $(document.body).outerWidth(true); ...
随机推荐
- 亚马逊开放机器学习系统源代码:挑战谷歌TensorFlow
北京时间5月17日上午消息,亚马逊在开源技术领域迈出了更大的步伐,宣布开放该公司的机器学习软件DSSTNE的源代码.这个最新项目将与谷歌的TensorFlow竞争,后者已于去年开源.亚马逊表示,在缺乏 ...
- 写一个ajax程序就是如此简单
写一个ajax程序就是如此简单 ajax介绍: 1:AJAX全称为Asynchronous JavaScript and XML(异步JavaScript和XML),指一种创建交互式网页应用的网页开发 ...
- [原创]Matlab之GUI生成EXE文件
近期因为项目需要,简化流程,写了一些Matlab程序,并配备上了GUI界面使其简单易用.然后问题来了,可移植性.使用Matlab生成EXE文件(可以封装很多的function),然后在一台安装有Mat ...
- 使用Aspose插件将程序中的表格,导出生成excel表格
http://www.cnblogs.com/lanyue52011/p/3372452.html这个是原文地址 /// <summary> /// 点击按钮,将内存表导出excel表格! ...
- JS控制鼠标点击事件
鼠标点击事件就是当鼠标点击元素时,就会出现另一个窗口,类似于百度首页中右上角的“登录”这个按钮,当鼠标点击 登录时,就会出现登录窗口.大体的意思就是这样,直接上代码了,简单易懂. <!DOCTY ...
- 关于Plupload结合上传插件jquery.plupload.queue的使用
之前使用过很多的上传组件,但对各种浏览器的兼容性太差,不得不放弃!! plupload 是款很强大的上传组件,不得不推荐.plupload 前端根据浏览器不同选择使用Html5. Gears, Sil ...
- 访问https链接方法
<a id='___szfw_logo___' href='https://credit.szfw.org/CX20160808028375110138.html' target='_blank ...
- Points on cycle
Description There is a cycle with its center on the origin. Now give you a point on the cycle, you a ...
- 存储构造题(Print Check)
连接:Print Check 题意:n行m列的矩阵,有k次涂色,每一涂一行或者一列,求最后的涂色结果. 从数据的大小看,暴力肯定要TLE: 问题是如何存储数据. 首先:我们只要最后的涂色结果. 其次: ...
- Java垃圾回收小结
一.如何确定某个对象是“垃圾”? 首先要搞清一个最基本的问题:如果确定某个对象是“垃圾”?既然垃圾收集器的任务是回收垃圾对象所占的空间供新的对象使用,那么垃圾收集器如何确定某个对象是“垃圾”?—即通过 ...