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); ...
随机推荐
- 判断Ie浏览器
ie8以下 if(!+[1,]) if(window.attachEvent){ alert("ie")}else if(window.addEventListener){aler ...
- 树莓派(raspberry pi)学习4: 更改键盘布局(转)
树莓派(raspberry pi)用了几次后,发现键盘老是按错,一些字符打不出来或打错 这个问题,折腾我半天.还是把心得分享一下吧 上网查,发现是键盘布局不对,树莓派(raspberry pi)是英国 ...
- js完美转换阿拉伯数字为数字大写(原创)
啥都不说,直接上代码: //阿拉伯数字转换为简写汉字 function Arabia_To_SimplifiedChinese(Num) { for (i = Num.length - 1; i &g ...
- 在updatepanel使用colorbox无效
今天在给一个使用colorbox的页面加了一个updatepanel后,colorbox无效了,原因是以前的页面初始化colorbox是用 $(document).ready(function(){ ...
- topsort | | jzoj[1226] | | NOIP2003神经网络
今天终于通过了那道永远都看不懂题目的神经网络... 所谓拓扑排序,就是在有向无环图中,根据已经有的点和点之间的关系进行排序 引用jzyz教材上的栗子:比如说奶牛比较食量大小,我现在拿到的是cow[i] ...
- sublime txt 设置在浏览器预览
1. 安装SideBarEnhancements插件 ctrl+shift+p —> Install Package —> 找到SideBarEnhancements 2. 配置预览快捷键 ...
- js闭包和回调
1.闭包 闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现.闭包有三个特性: 1.函数嵌套函数; 2.函数内部可以引用外部的参数和变量; 3.参数 ...
- 解决ubuntu bash: cd: ~:Permission denied
cd /usr/local/hadoop 报错:bash: cd: /usr/local/hadoop:Permission denied 输入指令: /usr/local/hadoop 原因是没有权 ...
- <转载> OpenGL Projection Matrix
原文 OpenGL Projection Matrix Related Topics: OpenGL Transformation Overview Perspective Projection Or ...
- 用C写的俄罗斯方块游戏 By: hoodlum1980 编程论坛
/************************************ * Desc: 俄罗斯方块游戏 * By: hoodlum1980 * Email: jinfd@126.com * Dat ...