typeof、instanceof、hasOwnProperty()、isPrototypeOf()
1.typeof
用于获取变量的类型,一般只返回以下几个值:string,number,boolean,object,function,undefined.
typeof作用于引用类型的时候,无论是什么类型的对象,都返回“object”.
alert(typeof false);//boolean
alert(typeof );//number
alert(typeof "hello Jame");
alert(typeof null);//object
alert(typeof [,,,]);//object
alert(typeof {name:"Jhon",age:});//object
alert(typeof function a(){});//function
alert(typeof new String("str"));//object
String 与 string的区别:一个是引用类型 object,一个是值类型 string。
var s = "hello Jame";
var Str = new String("hello Jhon");
alert(typeof s);//string
alert(typeof Str);//object
凡是通过 new 操作符创建的变量,typeof运算的结果都是object.
function myFun (){};
var a = new myFun();
alert(typeof a);//object
var b = function myFun1 (){};
alert(typeof b);//function
alert(typeof myFun1);//undefined
2.instanceof
用来判断一个实例是否属于某种类型。
更重的一点是 instanceof 可以在继承关系中用来判断一个实例是否属于它的父类型。
function ClassA(){}
function ClassB(){}
function ClassC(){}
/**
* 原型继承
* ClassB继承自ClassA,ClassC继承自ClassB
*/
ClassB.prototype = new ClassA();
ClassC.prototype = new ClassB();
/**
* 创建ClassB和ClassC的实例
*/
var b = new ClassB();
var c = new ClassC();
alert(b instanceof ClassB);//true
alert(b instanceof ClassA);//true
alert(c instanceof ClassA);//true
alert(c instanceof ClassB);//true
alert(c instanceof ClassC);//true
instanceof复杂用法
alert(Object instanceof Object);//true
alert(Function instanceof Function);//true alert(Function instanceof Object);//true alert(String instanceof String);//false
alert(Number instanceof Number);//false
alert(Boolean instanceof Boolean);//false
要从根本上了解instanceof,需从以下两个方面着手:1、语言规范中如何定义该运算符。2、JavaScript的原型继承机制。
推荐文章:JavaScript instanceof 运算符深入剖析
3.hasOwnProperty()方法
用来判断某实例是否具指定的名称属性或方法。是返回true,否则返回false。
注意:该属性只有是对象本身的一个成员才会返回true。不会检查原型链中是否有给定的方法或属性。
IE 5.5+、FireFox、Chrome、Safari、Opera等主流浏览器均支持该函数。
function ClassA(){
this.fname="张";
this.lname="三";
this.rank = ;
this.sayHello = function(){
alert("Hello:"+fname+lname);
};
}
var myPro = {
name:"张三",
age:
};
ClassA.prototype = myPro;
var a = new ClassA();
alert(a.hasOwnProperty("fname"));//true
alert(a.hasOwnProperty("lname"));//true
alert(a.hasOwnProperty("sayHello"));//true
alert(a.hasOwnProperty("name"));//false
4.isPrototypeOf()方法
判断某对象是否是指定实例的原型。是返回true,否则返回false。
注意:该方法不会检查原型链中的对象。即:只检查本实例的直接原型,不会检查本实例的原型的原型。
function ClassA(){}
function ClassB(){}
function ClassC(){}
/**
* ClassB的原型设为ClassA,ClassC的原型设为ClassB
* 注意:这与原型继承不同
*/
ClassB.prototype = ClassA;
ClassC.prototype = ClassB;
var b = new ClassB();
var c = new ClassC();
alert(ClassA.isPrototypeOf(b));//true
alert(ClassB.isPrototypeOf(c));//true
alert(ClassA.isPrototypeOf(c));//false
prototype和_proto_相连的两部分,用isPrototypeOf(),结果返回true.请结合原型链图体会下列代码的结果。
alert(Object.isPrototypeOf(Function));//false
alert(Object.prototype.isPrototypeOf(Function.prototype));//true function ClassD(){};
alert(Object.prototype.isPrototypeOf(ClassD.prototype));//true
alert(Function.prototype.isPrototypeOf(ClassD));//true var d = new ClassD();
alert(Object.prototype.isPrototypeOf(d.prototype));//false
alert(Object.isPrototypeOf(d));//false var obb = new Object();
alert(Object.prototype.isPrototypeOf(Object));//true
alert(Object.prototype.isPrototypeOf(obb));//true

引用:JavaScript instanceof 运算符深入剖析
typeof、instanceof、hasOwnProperty()、isPrototypeOf()的更多相关文章
- JavaScript中typeof,instanceof,hasOwnProperty,in的用法和区别
一. typeof操作符 typeof操作符用于返回正在使用值的类型. // 使用原始值 let mNull = null; let mUndefined = undefined; let mStri ...
- java基础9 main函数、this、static、super、final、instanceof 关键字
一.main函数详解 1.public:公共的.权限是最大的,在任何情况都可以访问 原因:为了保证jvm在任何情况下都可以访问到main法2.static:静态,静态可以让jvm调用更方便,不需要用 ...
- 面向对象的程序设计(二)理解各种方法和属性typeof、instanceof、constructor、prototype、__proto__、isPrototypeOf、hasOwnProperty
//理解各种方法和属性typeof.instanceof.constructor.prototype.__proto__.isPrototypeOf.hasOwnProperty. //1.typeo ...
- typeof、instanceof与isPrototypeOf()的差异与联系
一.typeof 1.typeof的意义及作用: 我们知道ECMAScript中有5种简单(基本)数据类型:Undefined.Null.Boolean.Number.String,以及一种引用数据类 ...
- constructor、prototype、isPrototypeOf、instanceof、in 、hasOwnProperty
constructor.prototype.isPrototypeOf.instanceof.in .hasOwnProperty等等 constructor:对象构造器.存在于原型对象中?,相当于p ...
- JavaScript中typeof、toString、instanceof、constructor与in
JavaScript 是一种弱类型或者说动态语言.这意味着你不用提前声明变量的类型,在程序运行过程中,类型会被自动确定. 这也意味着你可以使用同一个变量保存不同类型的数据. 最新的 ECMAScrip ...
- 【JavaScript中typeof、toString、instanceof、constructor与in】
JavaScript中typeof.toString.instanceof.constructor与in JavaScript 是一种弱类型或者说动态语言.这意味着你不用提前声明变量的类型,在程序运行 ...
- javascript 中isPrototypeOf 、hasOwnProperty、constructor、prototype等用法
hasOwnProperty:是用来判断一个对象是否有你给出名称的属性或对象,此方法无法检查该对象的原型链中是否具有该属性,该属性必须是对象本身的一个成员. isPrototypeOf是用来判断要检查 ...
- indexOf、instanceOf、typeOf、valueOf详解
1.indexOf() 该方法用来返回某个指定的字符串值在字符串中首次出现的位置. 语法:indexOf(searchvalue,fromindex);两个参数,参数一表示查询的字符串值,参数二可选表 ...
随机推荐
- MongoDB安装使用教程
参考菜鸟教程:http://www.runoob.com/mongodb/mongodb-tutorial.html
- Python之function
1 Function a function is a device that groups a set of statements so they can be run more than once ...
- BSGS-BabyStepGiantStep算法+拓展
学习数学真是一件赛艇的事. BSGS名字听起来非常有意思,力拔山兮气盖世,北上广深,小步大步...算法其实更有意思,它是用来求解一个方程的 A^x ≡ B (mod P) 是不是特别眼熟,有几个式子长 ...
- window 8 电脑操作服务集合(网址)
如何开启Win8远程桌面 http://jingyan.baidu.com/album/48206aeae06627216ad6b3bf.html?picindex=2 Win8.1用户账户的配置管理 ...
- HDU_1398_母函数
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- idea搭建第一个springboot
1.打开idea开发工具,在菜单栏选择File-->New-->Project...-->Spring Initializer说明:社区版的idea是没有Spring Initial ...
- c++ STL - priority_queue优先队列详解
简述 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有最高级先出 (first in, l ...
- 认识一下.net的架构设计
首先我们先逐步的了解一下.net都包含什么? 从层次结构上来看,公共语言运行时(CLR:Common Language Runtime).服务框架(Services Framework)和上层的两类应 ...
- Java Web学习总结(28)——Java Web项目MVC开源框架SSH和SSM比较
SSH和SSM定义 SSH 通常指的是 Struts2 做控制器(controller),spring 管理各层的组件,hibernate 负责持久化层. SSM 则指的是 SpringMVC 做控制 ...
- (36)Spring Boot Cache理论篇【从零开始学Spring Boot】
Spring Boot Cache理论篇 在上一篇中我们介绍了Spring Boot集成Redis的实战例子,里面使用到了Spring Cache,那么什么是Spring Cache呢,本章将会做一个 ...