如何从Object Prototype中获取实际的对象名称?

时间:2017-10-06 09:53:07

标签: javascript prototype

问题

我试图找出:如何log对象原型中的实际对象名称。

Object.prototype.contentAndname = function() {
  console.log()//what should be getting logged is bikes
}

transportation = {
  automobile: {
    cars: {
      nissan: ["Altima", "Leaf"],
      tesla: ["Model S", "Model X"],
    },
    trucks: {
      ford: ["Super Duty", "F-150"],
      chevy: ["Silverado", "Colorado"],
    }
  },
  bikes: {
    cheap: ['homemade'],
    luxury: ['gucci']
  },
  legs: {
    human: [],
    dogs: [],
    cats: []
  }
}


transportation.bikes.contentAndname()



结果我正在寻找

Object.prototype.contentAndname = function() {
  // Code that figures out that the object being pass through is called bikes.
  
  console.log('bikes')//what should be getting logged is bikes
}

transportation = {
  automobile: {
    cars: {
      nissan: ["Altima", "Leaf"],
      tesla: ["Model S", "Model X"],
    },
    trucks: {
      ford: ["Super Duty", "F-150"],
      chevy: ["Silverado", "Colorado"],
    }
  },
  bikes: {
    cheap: ['homemade'],
    luxury: ['gucci']
  },
  legs: {
    human: [],
    dogs: [],
    cats: []
  }
}


transportation.bikes.contentAndname()
// I'm passing bikes through prototype

0 个答案:

没有答案