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) ); // truevar 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); ...
随机推荐
- HTML 透明、阴影,圆角等知识点
table两个属性:cellpadding:内容与单元格边框的距离,内部距离cellspacing:单元格之间的距离,外部距离 table合并边框线: border-collapse: co ...
- Groovy basic
1. print println "Hello Groovy!" you can use java in Groovy System.out.println("Hello ...
- MCS-51系列特殊功能寄存器(摘抄)
1. P0 (80H) P0.7 P0.6 P0.5 P0.4 P0.3 P0.2 P0.1 P0.0 2.SP 栈指针(81H) 3.DPTR 数据指针(由DPH和DPL组成) DPL 数据指针低八 ...
- solr入门命令
#####################shell命令############################# 导入文档: sh bin/post -c gettingstarted docs/i ...
- Java 基础知识总结 (一、标识符)
一.Identifiers: 标识符 Names of class,method and variable 用于类名.方法名.变量名 Begin with character,'_' or '$' 标 ...
- sublime text保存时删除行尾空格
打开sublime text,点击在Preferences, Settings-User打开的用户配置中加入以下一行: "trim_trailing_white_space_on_save& ...
- 管道导致的while循环体变量失效
#!/bin/sh num= cat /etc/passwd | while read line do num=$(($num+)) done echo $num linux:~ # sh a.sh ...
- cinder backup
cinder 备份提供的驱动服务有: cinder/backup/drivers/ceph.py:def get_backup_driver(context): cinder/backup/drive ...
- upgrade-php-5-1-to-php-5-3-using-yum-on-centos
wget -q -O - http://www.atomicorp.com/installers/atomic | shyum upgrade phpyum -y remove atomic-rele ...
- iOS删除本地文件
以前在博客里记录的东西都是截屏,没有插入代码,今天进去一看,图片都不显示了,只好重新插入代码,发现以前写的文件操作这块,没有写本地文件删除这个功能,重新再记录一下 //需要删除文件的物理地址 NSSt ...