JavaScript hasOwnProperty vs typeof

Guy*_*uyT 3 javascript prototypal-inheritance

我在Google上搜索了很多但是找不到我要找的地方:

如果未定义Property,则使用Object.hasOwnProperty与测试的好处

jsperf speedtest

如何确定Native JavaScript Object是否具有属性/方法?

..以及很多其他网站,但这不是我要找的地方.我真正的问题是:

为什么hasOwnProperty在他的超类(原型)中找不到方法?为什么有人甚至会使用hasOwnProperty?它比typeof它慢得多,如果你正在使用继承它不起作用.

..第二个问题:

这个问题中,Barney回答你必须用来if ('property' in objectVar)检查属性是否存在,但不解释原因.有人知道你为什么要使用这种结构吗?

var objA = function(){};

objA.prototype.alertMessage = function(){
        return 'Hello A';
};

var objB = function(){
    this.hello = function(){
        return 'hello';
    };
};

// Inheritance
objB.prototype  = Object.create(objA.prototype);
objB.prototype.constructor = objA;

var test = new objB();

if (test.hasOwnProperty("alertMessage")){
    console.log("hasOwnProperty: " + test.alertMessage());
}

if (typeof test.alertMessage === "function"){
    console.log("typeof: " + test.alertMessage());
}

if (test.hasOwnProperty("hello")){
    console.log("hasOwnProperty: " + test.hello());
}

if (typeof test.hello === "function"){
    console.log("typeof: " + test.hello());
}
Run Code Online (Sandbox Code Playgroud)

看看jsFiddle

Eli*_*gem 9

使用(在某些情况下必须使用)的原因有很多hasOwnProperty,以及为什么它的行为是这样的:

  • 顾名思义,hasOwnProperty检查您测试的对象是否拥有具有给定名称的属性.如果它从另一个对象(其原型)继承了一个方法/属性,那么该属性的所有者不是对象,而是它的原型.因此,该对象没有自己的名为X的属性
  • typeof大多数时候都会工作,但是一个对象看起来像这样:var o = {foo: undefined}.当然,使用typeofon o.foo会产生"undefined",但对象确实拥有一个名为的属性foo
  • 使用if ('properyname' in object)是一种解决方案,它结合了两者的优点:在的情况下,o = {foo: undefined};评估为true,而不考虑查询的开销hasOwnPropery的方法(这是一个属性Object.prototype),或与上下文结合和所有的函数调用.它还将在原型链中找到属性

请考虑以下示例:

var o = {foo: undefined,
    toString: undefined};//overwrite inherited method
console.log(typeof o.foo);//undefined
console.log(typeof o.toString);//undefined
console.log(o.hasOwnProperty('toString'));//true
delete(o.toString);
console.log(typeof o.toString);//function
if ('valueOf' in o)
    console.log(o.valueOf === Object.prototype.valueOf);//true
if ('foo' in o)
    console.log(o.foo);//undefined
Run Code Online (Sandbox Code Playgroud)

另一个需要注意的重要事项是,关于hasOwnProperty在处理继承时不工作的声明是完全错误的.您在JS中使用的每个对象都至少从一个原型继承.重要的是要认识到,理解并尊重这一点.如果循环遍历对象,建议您确保实际迭代属于对象本身的属性.因此,这种情况并不罕见:

for (p in obj)
{
    if (obj.hasOwnProperty(p))
        //process property
}
Run Code Online (Sandbox Code Playgroud)

这是为了避免让代码迭代原型链中的所有属性,这可能会破坏超级对象

if (!Object.prototype.hasProperty)
{//dirty check ;-P
    Object.prototype.hasProperty = (function(OP)
    {
        return function(name)
        {
            //typeof for speed: if value is not undefined, return true
            if (typeof this[name] !== 'undefined' || this.hasOwnProperty(name))
                return true;
            if (this === OP)//we've just checked the Object.prototype, found nothing, so return false
                return false;
            return Object.getPrototypeOf(this).hasProperty(name);//check prototype
        };
    }(Object.prototype));//OP is object prototype
}
Run Code Online (Sandbox Code Playgroud)