JavaScript中的constructor、instanceof、isPrototypeOf、typeof以及hasOwnProperty
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JavaScript中的constructor、instanceof、isPrototypeOf、typeof以及hasOwnProperty,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4808字,纯文字阅读大概需要7分钟。
内容图文
![JavaScript中的constructor、instanceof、isPrototypeOf、typeof以及hasOwnProperty](/upload/InfoBanner/zyjiaocheng/1066/4f5069e6988445ae90ed75cfca393648.jpg)
1、hasOwnProperty和in
先来理解hasOwnProperty方法。这个方法是用来检查对象的非原型链属性,换句话说,也就是检查对象中用户自定义的属性,而且这些属性不是定义在prototype上的。通过下面的代码进行理解:
var myFunc=function(){ this.foo=‘value‘; }; myFunc.prototype.ok=‘ok‘; thefunc=new myFunc(); console.log( thefunc.hasOwnProperty(‘foo‘),//true thefunc.hasOwnProperty(‘ok‘),//false myFunc.hasOwnProperty(‘foo‘),//false myFunc.prototype.hasOwnProperty(‘ok‘),//true这里有点特殊 ‘ok‘ in thefunc,//true ‘foo‘ in thefunc,//true ‘ok‘ in myFunc.prototype,//true ‘ok‘ in myFunc//false );
在上面的代码中,foo是用户自定义的非原型属性,而ok则是定义在prototype上的原型属性,所以thefunc.hasOwnProperty(‘foo‘)的值为true,thefunc.hasOwnProperty(‘ok‘)的值为false。
JavaScript语言中还有一个in操作符,用来检查一个对象的属性,包括来自原型链的属性。
2、constructor
每一个JavaScript函数(ECMAScript 5中的Function.bind()返回的函数除外)都自动拥有一个prototype属性。这个属性的值是一个对象,这个对象包含唯一一个不可枚举的属性constructor。构造函数实例都拥有指向其构造函数的constructor属性。constructor属性的值是一个函数对象:
var F=function(){};//这是一个函数对象var P=F.prototype;//这是F相关联的原型对象var C=P.constructor;//这是与原型相关联的函数 C===F;//true,对于任意函数F.prototype.constructor===F //摘自JavaScript权威指南
在这里插入第一节里的代码,并扩展了一些,希望能够解释清楚构造函数和原型的关系:
var myFunc=function(){ this.foo=‘value‘; }; myFunc.prototype.ok=‘ok‘; thefunc=new myFunc(); console.log( thefunc.constructor===myFunc, //true myFunc.constructor===Function,//true typeof thefunc,//object myFunc.prototype//Object(){ok=‘ok‘} );
3、instanceof和isPrototypeOf
instanceof是JavaScript语言中的一种运算符。左操作数是待检测其类的对象,右操作数是定义类的构造函数。
isPrototypeOf是用来判断要检查其原型链的对象是否存在于指定对象实例中,是则返回true,否则返回false。
var myFunc=function(){ this.foo=‘value‘; }; myFunc.prototype.ok=‘ok‘; thefunc=new myFunc(); console.log( thefunc instanceof myFunc,//true myFunc instanceof Function,//true myFunc.prototype.isPrototypeOf(thefunc),//true Function.prototype.isPrototypeOf(myFunc),//true myFunc.prototype,//Object(){ok=‘ok‘}typeof thefunc,//object thefunc.prototype//undefined,这里没搞懂 );
4、typeof
typeof是一元运算符,放在操作数的前面,操作数可以是任意类型。返回值为表示操作数类型的一个字符串。
var myNumber = new Number(‘23‘); var myNumberL = 23; // literal shorthandvar myString = new String(‘male‘); var myStringL = ‘male‘; // literal shorthandvar myBoolean = new Boolean(‘true‘); var myBooleanL = true; // literal shorthandvar myObject = new Object(); var myObjectL = {}; // literal shorthandvar myArray = new Array(); var myArrayL = []; // literal shorthandvar myFunction = new Function(); var myFunctionL = function() {}; // literal shorthandvar myDate = new Date(); var myRegExp = new RegExp(‘/./‘); var myRegExpL = /./; // literal shorthandvar myError = new Error(); console.log( // all of these return true myNumber.constructor === Number, myNumberL.constructor === Number, myString.constructor === String, myStringL.constructor === String, myBoolean.constructor === Boolean, myBooleanL.constructor === Boolean, myObject.constructor === Object, myObjectL.constructor === Object, myArray.constructor === Array, myArrayL.constructor === Array, myFunction.constructor === Function, myFunctionL.constructor === Function, myDate.constructor === Date, myRegExp.constructor === RegExp, myRegExpL.constructor === RegExp, myError.constructor === Error ); console.log( //others return true myNumber instanceof Number, myNumberL instanceof Number,//false myString instanceof String, myStringL instanceof String,//false myBoolean instanceof Boolean, myBooleanL instanceof Boolean,//false myObject instanceof Object, myObjectL instanceof Object, myArray instanceof Array, myArrayL instanceof Array, myFunction instanceof Function, myFunctionL instanceof Function, myDate instanceof Date, myRegExp instanceof RegExp, myRegExpL instanceof RegExp, myError instanceof Error ); console.log( //others return true Number.prototype.isPrototypeOf(myNumber), Number.prototype.isPrototypeOf(myNumberL),//false String.prototype.isPrototypeOf(myString), String.prototype.isPrototypeOf(myStringL),//false Boolean.prototype.isPrototypeOf(myBoolean), Boolean.prototype.isPrototypeOf(myBooleanL),//false Object.prototype.isPrototypeOf(myObject), Object.prototype.isPrototypeOf(myObjectL), Array.prototype.isPrototypeOf(myArray), Array.prototype.isPrototypeOf(myArrayL), Function.prototype.isPrototypeOf(myFunction), Function.prototype.isPrototypeOf(myFunctionL), Date.prototype.isPrototypeOf(myDate), RegExp.prototype.isPrototypeOf(myRegExp), RegExp.prototype.isPrototypeOf(myRegExpL), Error.prototype.isPrototypeOf(myError) ); console.log( typeof myNumber,//objecttypeof myNumberL,//numbertypeof myString,//objecttypeof myStringL,//stringtypeof myBoolean,//objecttypeof myBooleanL,//booleantypeof myObject,//objecttypeof myObjectL,//objecttypeof myArray,//objecttypeof myArrayL,//objecttypeof myFunction,//functiontypeof myFunctionL,//functiontypeof myDate,////objecttypeof myRegExp,//objecttypeof myRegExpL,//objecttypeof myError//object );
原文:http://www.cnblogs.com/maplecumt/p/4682326.html
内容总结
以上是互联网集市为您收集整理的JavaScript中的constructor、instanceof、isPrototypeOf、typeof以及hasOwnProperty全部内容,希望文章能够帮你解决JavaScript中的constructor、instanceof、isPrototypeOf、typeof以及hasOwnProperty所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。