javascript--hasOwnProperty()+isPrototypeof()+in方法
1.hasOwnProperty():用来检测对象中是否包含给出的函数属性或者对象,但是无法检测出对象的原型链中是否包含给出的属性或对象--该属性或者对象必须在函数内部被声明
2.isPrototypeof():检查对象的原型链中是否包含给定的属性或者对象
<script> function f1 (age,sex) { this.age = age; this.sex = name; } f1.prototype.show = function() { alert('one'); }; f1.prototype.getage = function() { alert(this.age); }; var k1 = new f1("12","men"); var k2 = new f1("11","women"); //alert(k1.hasOwnProperty('show'));//false //alert(k1.hasOwnProperty('age'));//ture //alert(f1.prototype.isPrototypeOf(k1));//true //alert(f1.prototype.isPrototypeOf(k2));//false //alert(f1.prototype.hasOwnProperty('show'));//true //alert(f1.prototype.hasOwnProperty('age'));//false </script>
例子:
<script> // 对象 var man = { hands: 2, legs: 2, heads: 1 }; // 在代码的某个地方 // 一个方法添加给了所有对象 if (typeof Object.prototype.clone === "undefined") { Object.prototype.clone = function () {}; } for (var i in man) { if (man.hasOwnProperty(i)) { // 过滤 console.log(i, ":", man[i]); } } </script>
火狐控制台输出
hands : 2 legs : 2 heads : 1
for (var i in man) { console.log(i, ":", man[i]); }
输出
hands : 2 legs : 2 heads : 1 clone: function()
for (var i in man) { if (Object.prototype.hasOwnProperty.call(man, i)) { // 过滤 console.log(i, ":", man[i]); } }//main调用了Object.prototype.hasOwnProperty
输出
hands : 2 legs : 2 heads : 1
一个实例:
<script> var Person = function () { this.name = "abc"; }; Person.prototype.sayname = function () { alert(this.name); } var p = new Person(); //实例对象检测 console.log(p.hasOwnProperty('sayname')); //false console.log(p.hasOwnProperty('name')); //true //原型对象检测 console.log(Person.prototype.isPrototypeOf(p)); //true //实例对象或者原型对象都可以 console.log('sayname' in p); //true console.log('name' in p); //true </script>
javascript--hasOwnProperty()+isPrototypeof()+in方法的更多相关文章
- javascript 中isPrototypeOf 、hasOwnProperty、constructor、prototype等用法
hasOwnProperty:是用来判断一个对象是否有你给出名称的属性或对象,此方法无法检查该对象的原型链中是否具有该属性,该属性必须是对象本身的一个成员. isPrototypeOf是用来判断要检查 ...
- javascript的isPrototypeOf函数的理解
JavaScript中isPrototypeOf函数方法是返回一个布尔值,指出对象是否存在于另一个对象的原型链中.使用方法: object1.isPrototypeOf(object2)~~~原型链理 ...
- JavaScript中isPrototypeOf函数
转自:http://www.ijavascript.cn/shouce/javascript-isprototypeof-247.html JavaScript中 isPrototypeOf 函数方法 ...
- Javascript创建对象几种方法解析
Javascript创建对象几种方法解析 Javascript面向对象编程一直是面试中的重点,将自己的理解整理如下,主要参考<Javascript高级程序设计 第三版>,欢迎批评指正. 通 ...
- Android和JavaScript相互调用的方法
转载地址:http://www.jb51.net/article/77206.htm 这篇文章主要介绍了Android和JavaScript相互调用的方法,实例分析了Android的WebView执行 ...
- JavaScript document属性和方法
JavaScript document属性和方法 --------------------------------------------属性: 1. Attributes 存储节点的属性列表 ...
- 将JavaScript 插入网页的方法
将JavaScript 插入网页的方法 使用Javascript代码. 插入JavaScript 与在网页中插入CSS的方式相似.使用下面的代码可以在网页中插入JavaScript: ... 其中的. ...
- JavaScript常用对象的方法和属性
---恢复内容开始--- 本文将简单介绍JavaScript中一些常用对象的属性和方法,以及几个有用的系统函数. 一.串方法 JavaScript有强大的串处理功能,有了这些串方法,才能编写出丰富多彩 ...
- js 判断数组包含某值的方法 和 javascript数组扩展indexOf()方法
var questionId = []; var anSwerIdValue = []; ////javascript数组扩展indexOf()方法 Array.prototype.indexOf ...
- Javascript调用C#后台方法及JSon解析
Javascript调用C#后台方法及JSon解析 如何使用Ajax 调用C# 后台方法. 本文目录 如何使用Ajax 调用C# 后台方法. 1.后台(.cs)测试方法 2.前台调用(javasc ...
随机推荐
- material design 的android开源代码整理
material design 的android开源代码整理 1 android (material design 效果的代码库) 地址请点击:MaterialDesignLibrary 效果: 2 ...
- odd or even?
public boolean isEven(int data){ if((data&1)== 0) return true; return false; } much faster than ...
- 转 spring security的使用
[转自:http://haohaoxuexi.iteye.com/blog/2154714] 关于登录 目录 1.1 form-login元素介绍 1.1.1 使用自定义登录页面 1.1 ...
- 无法加载shockwave flash
热心网友 360浏览器的话,浏览器——工具——选项(非Internet选项)——高级设置——FLASH, 默认使用PPAPI Flash(需要重启浏览器) 默认使用NPAPI Flash(需要重启浏览 ...
- C#入门经典第十章接口的实现
- 网页中如何用 CSS 设置打印分页符
Word 中按 Ctrl + Enter 创建一个分页符,方便打印,其实网页中也可以,用 CSS 的 page-break-after:always;. <p>第 1 页</p> ...
- HDU 1883 Phone Cell(计算几何)
方法:选取一个点A,以点A为圆心做一个半径为r的圆,然后枚举另一个点B,以B为圆心做一个圆,如果这两个圆有交集,那我们在这个交集内选取一个点做半径为r的圆,这个圆就包括了A和B点,找到交集最多的区域并 ...
- Fastjson简单使用方法
一.简单数据的序列化 pubic class UserInfo implements Serializable{ private String name; private int age; publi ...
- FCKeditor文字编辑器
FCKeditor文字编辑器的js调用1.需要fckeditor包 下载地址 http://dl.pconline.com.cn/html_2/1/776/id=48351&pn=0.html ...
- PAT (Advanced Level) 1012. The Best Rank (25)
简单排序题. 注意:分数相同的人排名相同. #include<iostream> #include<cstring> #include<cmath> #includ ...