简体   繁体   中英

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. 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) . This will display properties that do not belong only to the prototype (which I assumed was your goal from the use of 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" ) .

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

Currently, you are always checking whether your object has an own property 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 .

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? 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. So there is no effect of hasOwnProperty at all.

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. Instead just use in (like '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. Since you are negating the expression obj.hasOwnProperty("c") it will always be true, thus pushing every key in your array. Maybe you want something like this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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