如何判断一个对象是否属于某个类?
发布日期:2021-05-10 02:09:13 浏览次数:15 分类:精选文章

本文共 3197 字,大约阅读时间需要 10 分钟。

������������������������������������������������

��� JavaScript ���������������������������������������������������������������������������������������instanceof ������������������������������������������������������������������������������������������ Object ������������������������ typeof ������������������������������������������������ [object Object]��������������������������������������������� Object.prototype.toString.call() ���������������������������������

instanceof ���������

instanceof ��������������������������������������������������������������������������������������������������������������������������������������������������� a ��������� Person ���������������������

if (a instanceof Person) {
alert('yes');
}

������������������������������������������������������������������������������ is ������������������

Object.prototype.toString.call() ������

������ Object ��������������������� toString() ��������������� [object Object]��������������������������������������������������������������������������������������������� Object.prototype.toString.call() ��������������������������������������������� toString ������������������������������������������������������

��������������������� Object.prototype.toString.call() ������������

Object.prototype.toString.call('a string');
// [object String]
Object.prototype.toString.call(1);
// [object Number]
Object.prototype.toString.call(true);
// [object Boolean]
Object.prototype.toString.call(undefined);
// [object Undefined]
Object.prototype.toString.call(null);
// [object Null]

��������������������������������������������������������������������������������������� []��������������� [object Array]���

������������������������

  • ��������� (String ������)���

    Object.prototype.toString.call('hello world'); // [object String]
  • ������ (Number ������)���

    Object.prototype.toString.call(123); // [object Number]
  • ��������� (Boolean ������)���

    Object.prototype.toString.call(true);
    // [object Boolean]
  • undefined ������

    Object.prototype.toString.call(undefined); // [object Undefined]
  • null ������

    Object.prototype.toString.call(null); // [object Null]
  • ������������ (Array ������)���

    Object.prototype.toString.call([]); // [object Array]
  • **��������������� (RegExp ������)���

    Object.prototype.toString.call(new RegExp()); // [object RegExp]
  • ������ (Date ������)���

    Object.prototype.toString.call(new Date()); // [object Date]
  • ������ (Function ������)���

    Object.prototype.toString.call(function() { });
    // [object Function]
  • ������ (Error ������)���

    Object.prototype.toString.call(new Error()); // [object Error]
  • ������������ (HTMLDocument ������)���

    Object.prototype.toString.call(document); // [object HTMLDocument]
  • ������������ (Window ������)���

    Object.prototype.toString.call(window); // [object Window]
  • ������������ (global ������)���

    Object.prototype.toString.call(global); // [object global]

###���������yt

���������������������������������������������������������������������instanceof ������������������������������������������ Object.prototype.toString.call() ���������������������������������������������������������������������������������������������������������������������������������������������������������������������

上一篇:修改Vue数组的三组方法
下一篇:如何修改chrome记住密码后自动填充表单的黄色背景

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月06日 19时43分00秒