无为清净楼资源网 Design By www.qnjia.com
在 JavaScript 中,判断一个变量的类型尝尝会用 typeof 运算符,在使用 typeof 运算符时采用引用类型存储值会出现一个问题,无论引用的是什么类型的对象,它都返回 “object”。这就需要用到instanceof来检测某个对象是不是另一个对象的实例。
通常来讲,使用 instanceof 就是判断一个实例是否属于某种类型。
另外,更重的一点是 instanceof 可以在继承关系中用来判断一个实例是否属于它的父类型。
复制代码 代码如下:
// 判断 foo 是否是 Foo 类的实例 , 并且是否是其父类型的实例function Aoo(){}
function Foo(){}
Foo.prototype = new Aoo();//JavaScript 原型继承
var foo = new Foo();
console.log(foo instanceof Foo)//true
console.log(foo instanceof Aoo)//true
上面的代码中是判断了一层继承关系中的父类,在多层继承关系中,instanceof 运算符同样适用。
instanceof 复杂用法
复制代码 代码如下:
function Cat(){}
Cat.prototype = {}
function Dog(){}
Dog.prototype ={}
var dog1 = new Dog();
alert(dog1 instanceof Dog);//true
alert(dog1 instanceof Object);//true
Dog.prototype = Cat.prototype;
alert(dog1 instanceof Dog);//false
alert(dog1 instanceof Cat);//false
alert(dog1 instanceof Object);//true;
var dog2= new Dog();
alert(dog2 instanceof Dog);//true
alert(dog2 instanceof Cat);//true
alert(dog2 instanceof Object);//true
Dog.prototype = null;
var dog3 = new Dog();
alert(dog3 instanceof Cat);//false
alert(dog3 instanceof Object);//true
alert(dog3 instanceof Dog);//error
要想从根本上了解 instanceof 的奥秘,需要从两个方面着手:1,语言规范中是如何定义这个运算符的。2,JavaScript 原型继承机。大家感兴趣的可以去查看相关资料。
通常来讲,使用 instanceof 就是判断一个实例是否属于某种类型。
另外,更重的一点是 instanceof 可以在继承关系中用来判断一个实例是否属于它的父类型。
复制代码 代码如下:
// 判断 foo 是否是 Foo 类的实例 , 并且是否是其父类型的实例function Aoo(){}
function Foo(){}
Foo.prototype = new Aoo();//JavaScript 原型继承
var foo = new Foo();
console.log(foo instanceof Foo)//true
console.log(foo instanceof Aoo)//true
上面的代码中是判断了一层继承关系中的父类,在多层继承关系中,instanceof 运算符同样适用。
instanceof 复杂用法
复制代码 代码如下:
function Cat(){}
Cat.prototype = {}
function Dog(){}
Dog.prototype ={}
var dog1 = new Dog();
alert(dog1 instanceof Dog);//true
alert(dog1 instanceof Object);//true
Dog.prototype = Cat.prototype;
alert(dog1 instanceof Dog);//false
alert(dog1 instanceof Cat);//false
alert(dog1 instanceof Object);//true;
var dog2= new Dog();
alert(dog2 instanceof Dog);//true
alert(dog2 instanceof Cat);//true
alert(dog2 instanceof Object);//true
Dog.prototype = null;
var dog3 = new Dog();
alert(dog3 instanceof Cat);//false
alert(dog3 instanceof Object);//true
alert(dog3 instanceof Dog);//error
要想从根本上了解 instanceof 的奥秘,需要从两个方面着手:1,语言规范中是如何定义这个运算符的。2,JavaScript 原型继承机。大家感兴趣的可以去查看相关资料。
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年10月04日
2024年10月04日
- 邓丽君.1983-漫步人生路(2024环球MQA-UHQCD限量版)【环球】【WAV+CUE】
- 陈容森.1996-情断【上华】【WAV+CUE】
- 裘海正.1994-爱我的人和我爱的人【上华】【WAV+CUE】
- 庾澄庆.1988-错过的爱【福茂】【WAV+CUE】
- 班得瑞原装进口《第十三张新世纪专辑:旭日之丘》1CD[APE/CUE分轨][329.6MB]
- 纯音入心系列纯音乐《古筝|佛蕴|境法禅心意法自然》1CD[MP3][350MB]
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[320K/MP3][203.44MB]
- 群星.1981-名曲65(2014环球复黑王·百代篇)【EMI百代】【WAV+CUE】
- 陈淑桦.1990-娃娃的故事【柯达】【WAV+CUE】
- 戴梅君.2011-问签诗【美华】【WAV+CUE】
- 李国祥.1995-九五变奏【嘉音】【WAV+CUE】
- 许景淳.1992-你来自何方【全美唱片】【WAV+CUE】
- 石欣卉.2007-剧欣卉集·完整电视剧主题精丫华纳】【WAV+CUE】
- 群星.2005-LOVE情歌集VOL.5.2CD【正东】【WAV+CUE】
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】