简体   繁体   English

hasOwnProperty-原型-不起作用

[英]hasOwnProperty - prototype - doesn't work

I am trying to exclude the property c if found so it won't be added to the properties array, however, it is being added still. 我试图排除找到的属性c,因此不会将其添加到properties数组中,但是仍在添加中。 Why? 为什么?

var letters = function () {
  this.a = 5;
  this.b = 20;
};

letters.prototype = {
  c: 10
};

var letters = new letters();

function looping(obj) {
  var properties = [];
  for (var key in obj) {
    if (!obj.hasOwnProperty("c")) {

      properties.push(key);
    }
  }
  return properties;

}

looping(letters);

I changed !obj.hasOwnProperty("c") to obj.hasOwnProperty(key) . 我将!obj.hasOwnProperty("c")更改为obj.hasOwnProperty(key) This will display properties that do not belong only to the prototype (which I assumed was your goal from the use of obj.hasOwnProperty ). 这将显示不仅仅属于原型的属性(我认为这是使用obj.hasOwnProperty目标)。 If, as some other answers assume, your goal is to exclude only the property "c" , you could change if condition to if ( key !== "c" ) . 如其他答案所假定的那样,如果您的目标是仅排除属性"c" ,则可以将if条件更改为if ( key !== "c" )

var letters = function () {
  this.a = 5;
  this.b = 20;
};

letters.prototype = {
  c: 10
};

var letters = new letters();

function looping(obj) {
  var properties = [];
  for (var key in obj) {
    if (obj.hasOwnProperty(key)) {

      properties.push(key);
    }
  }
   return properties;

}

 looping(letters);

I think what you wanted to say is something like this 我想你想说的是这样的

 var Letters = function() { this.a = 5; this.b = 20; }; Letters.prototype = { c: 10 }; var letters = new Letters(); function looping(obj) { var properties = []; for (var key in obj) { if (key !== "c") { properties.push(key); } } return properties; } alert(looping(letters)); 

You should also change constructor function letters to be Letters , with capital "l", and not use the same name for both constructor and instance 您还应该将构造函数的字母更改为Letters ,并使用大写字母“ l”,并且不要对构造函数和实例使用相同的名称

Currently, you are always checking whether your object has an own property c . 当前,您始终在检查对象是否具有自己的属性c Since c is in the prototype chain of your letters instance (with which you are overriding your function reference, by the way!), this check will always result in false . 由于c在您的letter实例的原型链中(顺便说一下,您将使用它们重写函数引用!),所以此检查将始终导致false

I'm not entirely sure if you only want to exclude c on the prototype chain specifically or if you want to exclude all properties from the prototype? 我不确定是要只在原型链上专门排除c还是要从原型中排除所有属性? I'm going to assume the first option in the following code: 我将假设以下代码中的第一个选项:

var Letters = function () {
  this.a = 5;
  this.b = 20;
};

Letters.prototype = {
  c: 10
};

var myLetters = new Letters();

function looping(obj) {
  var properties = [];
  for (var key in obj) {
    if (key !== 'c' || obj.hasOwnProperty(key)) {
      properties.push(key);
    }
  }
  return properties;

}

looping(myLetters);

the for loop will go through all the keys from the actual object and its prototype and you are actually sabotaging hasOwnProperty by using "c" in the call obj.hasOwnProperty("c") and negating it. for循环将遍历实际对象及其原型中的所有keys ,实际上您是通过在调用obj.hasOwnProperty("c")使用"c"并对其进行否定来破坏hasOwnProperty的。 So there is no effect of hasOwnProperty at all. 因此,根本没有hasOwnProperty作用。

The right way to do is: 正确的方法是:

function looping(obj){
    var properties=[];
    for (var key in obj){
        if (obj.hasOwnProperty(key)) {

            properties.push(key);
        }
    }
        return properties;
}
console.log(looping(letters));  //prints ["a", "b"]

hasOwnProperty method doesn't look up for property in prototype chain. hasOwnProperty方法不在原型链中查找属性。 Instead just use in (like 'c' in obj ). 而是只使用in (例如'c' in obj )。

The issue here is that you are checking if the c exists as property within your obj which will always be false since your obj wont have the " ownProperty " c by prototype functionality as far as i know. 这里的问题是,您正在检查c是否作为obj中的属性存在,因为根据我所知,您的obj不会通过原型功能获得“ ownProperty ” c,因此它将始终为false Since you are negating the expression obj.hasOwnProperty("c") it will always be true, thus pushing every key in your array. 由于您要否定表达式obj.hasOwnProperty("c")因此它将始终为true,从而将数组中的每个键都压入。 Maybe you want something like this. 也许您想要这样的东西。

function looping(obj) {
  var properties = [];
  for (var key in obj) {
    if(key !== "c") properties.push(key);
  }
  return properties;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM