为什么在这个 reduce 回调中使用 Object.create?
Why use Object.create inside this reduce callback?
因此,在完成本精美教程的第 19 篇 - http://jhusain.github.io/learnrx/ 时,我发现该练习无需使用 Object.create 即可完成。 (请参阅注释掉的行)
1。那么创建 accumulatedMap 的副本有什么意义呢?除了证明这是可能的...
function() {
var videos = [
{
"id": 65432445,
"title": "The Chamber"
},
{
"id": 675465,
"title": "Fracture"
},
{
"id": 70111470,
"title": "Die Hard"
},
{
"id": 654356453,
"title": "Bad Boys"
}
];
// Expecting this output...
// [
// {
// "65432445": "The Chamber",
// "675465": "Fracture",
// "70111470": "Die Hard",
// "654356453": "Bad Boys"
// }
// ]
return videos.
reduce(function(accumulatedMap, video) {
// Object.create() makes a fast copy of the accumulatedMap by
// creating a new object and setting the accumulatedMap to be the
// new object's prototype.
// Initially the new object is empty and has no members of its own,
// except a pointer to the object on which it was based. If an
// attempt to find a member on the new object fails, the new object
// silently attempts to find the member on its prototype. This
// process continues recursively, with each object checking its
// prototype until the member is found or we reach the first object
// we created.
// If we set a member value on the new object, it is stored
// directly on that object, leaving the prototype unchanged.
// Object.create() is perfect for functional programming because it
// makes creating a new object with a different member value almost
// as cheap as changing the member on the original object!
//var copyOfAccumulatedMap = Object.create(accumulatedMap);
//copyOfAccumulatedMap[video.id] = video.title;
accumulatedMap[video.id] = video.title;
//return copyOfAccumulatedMap;
return accumulatedMap;
},
// Use an empty map as the initial value instead of the first item in
// the list.
{});
}
2 如果你做 使用 copyOfAccumulatedMap
做它,你最终会得到一个像这样的对象
这看起来很有趣,但令人费解,因为我不能用这里的代码做同样的事情
var fruit = { 'taste' : 3 };
var apple = Object.create(fruit);
apple['size'] = 7;
var apple = Object.create(fruit);
apple['hardness'] = 6;
var apple = Object.create(fruit);
apple['weight'] = 10;
console.log(apple.taste);
console.log(apple.size); // undefined as I would expect
console.log(apple.hardness); // undefined as I would expect
console.log(apple.weight);
那么是什么让练习中的对象拥有所有链式原型但不允许我的苹果这样做呢?
你有点回答了你自己的问题:Object.create
制作了那个原型链。 Object.create
将正在创建的对象的原型对象作为其参数。
var copyOfAccumulatedMap = Object.create(accumulatedMap);
旧 accumulatedMap
被用作 copyOfAccumulatedMap
的原型。然后copy返回,作为下一次通过的accumulatedMap
。
因此,在完成本精美教程的第 19 篇 - http://jhusain.github.io/learnrx/ 时,我发现该练习无需使用 Object.create 即可完成。 (请参阅注释掉的行)
1。那么创建 accumulatedMap 的副本有什么意义呢?除了证明这是可能的...
function() {
var videos = [
{
"id": 65432445,
"title": "The Chamber"
},
{
"id": 675465,
"title": "Fracture"
},
{
"id": 70111470,
"title": "Die Hard"
},
{
"id": 654356453,
"title": "Bad Boys"
}
];
// Expecting this output...
// [
// {
// "65432445": "The Chamber",
// "675465": "Fracture",
// "70111470": "Die Hard",
// "654356453": "Bad Boys"
// }
// ]
return videos.
reduce(function(accumulatedMap, video) {
// Object.create() makes a fast copy of the accumulatedMap by
// creating a new object and setting the accumulatedMap to be the
// new object's prototype.
// Initially the new object is empty and has no members of its own,
// except a pointer to the object on which it was based. If an
// attempt to find a member on the new object fails, the new object
// silently attempts to find the member on its prototype. This
// process continues recursively, with each object checking its
// prototype until the member is found or we reach the first object
// we created.
// If we set a member value on the new object, it is stored
// directly on that object, leaving the prototype unchanged.
// Object.create() is perfect for functional programming because it
// makes creating a new object with a different member value almost
// as cheap as changing the member on the original object!
//var copyOfAccumulatedMap = Object.create(accumulatedMap);
//copyOfAccumulatedMap[video.id] = video.title;
accumulatedMap[video.id] = video.title;
//return copyOfAccumulatedMap;
return accumulatedMap;
},
// Use an empty map as the initial value instead of the first item in
// the list.
{});
}
2 如果你做 使用 copyOfAccumulatedMap
做它,你最终会得到一个像这样的对象
这看起来很有趣,但令人费解,因为我不能用这里的代码做同样的事情
var fruit = { 'taste' : 3 };
var apple = Object.create(fruit);
apple['size'] = 7;
var apple = Object.create(fruit);
apple['hardness'] = 6;
var apple = Object.create(fruit);
apple['weight'] = 10;
console.log(apple.taste);
console.log(apple.size); // undefined as I would expect
console.log(apple.hardness); // undefined as I would expect
console.log(apple.weight);
那么是什么让练习中的对象拥有所有链式原型但不允许我的苹果这样做呢?
你有点回答了你自己的问题:Object.create
制作了那个原型链。 Object.create
将正在创建的对象的原型对象作为其参数。
var copyOfAccumulatedMap = Object.create(accumulatedMap);
旧 accumulatedMap
被用作 copyOfAccumulatedMap
的原型。然后copy返回,作为下一次通过的accumulatedMap
。