如何从原型中查找对象值

时间:2017-06-23 00:12:39

标签: javascript json

是否可以创建一个可以从对象的原型中找到的函数? 如何在Object的原型上找到值“5”以及使用什么算法?

var rex = {
"Name": "rex",
"Age": 16,
}
te = {
 "to": 5,
}

rex.te = Object.create(te);

function find(a,b){
	console.log(rex.te.to);
}
find(5,rex);
console.log(rex);

如何在Object的原型上找到值“5”?

2 个答案:

答案 0 :(得分:1)

您所展示的内容不是对象的原型。它们只是对象值。原型本质上是对象上已存在的默认函数。

如果要查找对象的值是否为16,则需要循环访问该对象并检查每个键值对等体。这里还有其他一些答案:
How to check if value exists in object using javascript

答案 1 :(得分:0)

let fetchOk = (...args) => fetch(...args)
  .then(res => res.ok ? res : res.json().then(data => {
    throw Object.assign(new Error(data.error_message), {name: res.statusText});
  }));

Promise.all([
  'http://localhost:3000/incomplete',
  'http://localhost:3000/complete'
].map(url => fetchOk(url).then(r => r.json()))).then(([d1, d2]) => {
  // do stuff with d1 and d2
}).catch(e => console.error(e));


// Work around stackoverflow's broken error logger.
var console = { error: msg => div.innerHTML += msg + "<br>" };