![jquery中获取对象类型字符串的代码,class2type[ tostring.call( obj ) ]的作用是什么?](https://www.chuangxiangniao.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
jquery中获取对象类型字符串的代码,tostring.call(obj)详解
问题:
在jquery获取对象类型字符串的代码中,class2type[ tostring.call( obj ) ]这一行代码的作用是什么?
代码:
var class2type = {};var tostring = class2type.tostring;function totype( obj ) { if ( obj == null ) { return obj + ""; } return typeof obj === "object" ? class2type[ tostring.call( obj ) ] || "object" : typeof obj;}
答案:
class2type.tostring等同于object.prototype.tostring,它用以获取对象的类型字符串。
tostring.call( obj )调用了对象的tostring方法,该方法会返回对象的类型字符串。如“[object object]”、“[object array]”、“[object function]”等。
在totype函数中,tostring.call( obj )获取的对象类型字符串被用作class2type对象的键。class2type对象存储了不同的类型字符串与其对应的类型名称。
例如:
class2type["[object Object]"] = "object";class2type["[object Array]"] = "array";class2type["[object Function]"] = "function";
因此,class2type[ tostring.call( obj ) ]返回对象的类型名称。它用于确定对象的类型,以便可以对其执行特定操作。
以上就是jQuery中获取对象类型字符串的代码,class2type[ toString.call( obj ) ]的作用是什么?的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1498045.html
微信扫一扫
支付宝扫一扫