如何在向Object添加原型时排除一些js类型

How to Exclude some js types when adding prototype to Object

本文关键字:排除 js 类型 原型 Object 添加      更新时间:2023-09-26

一旦我将以下代码添加到我的html页面,我就会得到:

Uncaught TypeError: Object function (constrname) {
    if(this.constructor.name===constrname){
        return true;
    }
    return false;
} has no method 'exec'

这是导致错误的原型:

Object.prototype.isInstanceOf=function(constrname) {
    if(this.constructor.name===constrname){
        return true;
    }
    return false;
};

添加原型似乎破坏了jQuery。

在将新原型定义为对象类型时,我可以排除某些类型吗。例如,exclude:jQuery type,。。。

我尝试了以下代码,但没有成功:

Object.prototype.isInstanceOf=function(constrname) {
      if(!(this instanceof jQuery)){
              //write here code
      }
}

不,这是不可能的。原型继承的全部意义在于原型上的对象由每个后代继承。无法删除它们。