1

Found answer here: Javascript what is property in hasOwnProperty?

In this post How to display all methods of an object?, it said "You can use Object.getOwnPropertyNames() to get all properties that belong to an object, whether enumerable or not". And with the example we can see all the properties includes methods of the Math object listed. I tried and got the same result.

Then I tried to define my own object and list it's properties in the same way, but why the methods are not listed? e.g. For console.log(Object.getOwnPropertyNames(animal)) why it only returns ["name", "weight"] but not includes "eat", "sleep" and wakeUp?

function Animal(name, weight) {
    this.name = name;
    this.weight = weight;
}

Animal.prototype.eat = function() {
    return `${this.name} is eating!`;
}

Animal.prototype.sleep = function() {
    return `${this.name} is going to sleep!`;
}

Animal.prototype.wakeUp = function() {
    return `${this.name} is waking up!`;
}

var animal = new Animal('Kitten', '5Kg');
console.log(Object.getOwnPropertyNames(animal));  // ["name", "weight"]

And another example is, why the following one returns the property which belongs to the super class, as it's Triangle inherited from Shape. For console.log(Object.getOwnPropertyNames(triangle)); do we suppose only get ["a", "b", "c"] without "type"?

class Shape {
  constructor(type) {
    this.type = type;
  }
  getType() {
    return this.type;
  }
}

class Triangle extends Shape {
  constructor(a, b, c) {
    super("triangle");
    this.a = a;
    this.b = b;
    this.c = c;
  }
  getParamiter() {
    return this.a + this.b + this.c;
  }
}

const triangle = new Triangle(1,2,3);
console.log(Object.getOwnPropertyNames(triangle));   //["type", "a", "b", "c"]
Barmar
  • 741,623
  • 53
  • 500
  • 612
Lisa
  • 150
  • 2
  • 9
  • 3
    `getOwnPropertyNames()` won't return properties that are inherited from the prototype. That's what "own" means -- it only returns the properties that are in the object itself. – Barmar Mar 09 '19 at 00:47
  • 1
    See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties for options to get more properties/methods – Cat Mar 09 '19 at 00:49
  • This part of [You don't Know JS](https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch5.md#review-tldr) book explains what's happened and how the prototype methods(they are just another objects) are *linked* to the object that you are creating with `new`. It explains how foundementally prototype inheritance works. – hcheung Mar 09 '19 at 01:00

1 Answers1

7

All the object methods with "own" in the name only look at properties that are directly in the object, not properties that are inherited from prototypes.

You can use Object.getPrototypeOf() to get the prototype, and then call Object.getOwnProperties() on that.

That will only get the direct prototype's methods. If you want the entire chain, you'll need to write a loop that keeps calling getPrototypeOf() until it reaches Object.

function Animal(name, weight) {
    this.name = name;
    this.weight = weight;
}

Animal.prototype.eat = function() {
    return `${this.name} is eating!`;
}

Animal.prototype.sleep = function() {
    return `${this.name} is going to sleep!`;
}

Animal.prototype.wakeUp = function() {
    return `${this.name} is waking up!`;
}


function Gorilla(name, weight) {
    Animal.call(this, name, weight);
}

Gorilla.prototype = Object.create(Animal.prototype);
Gorilla.prototype.constructor = Gorilla;

Gorilla.prototype.climbTrees = function () {
    return `${this.name} is climbing trees!`;
}

Gorilla.prototype.poundChest = function() {
    return `${this.name} is pounding its chest!`;
}

Gorilla.prototype.showVigour = function () {
    return `${Animal.prototype.eat.call(this)} ${this.poundChest()}`;
}

Gorilla.prototype.dailyRoutine = function() {
    return `${Animal.prototype.wakeUp.call(this)} ${this.poundChest()} ${Animal.prototype.eat.call(this)} ${Animal.prototype.sleep.call(this)}`;
}

var animal = new Animal('Kitten', '5Kg');
console.log(Object.getOwnPropertyNames(animal));  // ["name", "weight"]
console.log(Object.getOwnPropertyNames(Object.getPrototypeOf(animal)));
var gorilla = new Gorilla('George', '160Kg');
console.log(Object.getOwnPropertyNames(gorilla)); console.log(Object.getOwnPropertyNames(Object.getPrototypeOf(gorilla)));
Barmar
  • 741,623
  • 53
  • 500
  • 612