0

Trying to return property using closure, keep getting undefined. What am I not understanding? I have a similar code that works on an [];.

var myObject = {
name: 'Mark P Jaramillo',
city: 'Petaluma',
state: 'California'
};

var closureObjPrint = function (object) {     
  return function(key) {
    return object.key;
    }
}

var obj = closureObjPrint(myObject);

console.log( obj(name) );
  • I do not see how this is a duplicate to dot and bracket notation when neither works in my closure. – mark jaramillo Aug 22 '15 at 04:17
  • That's where your problem is. The code works if you use `return object[key]` and `console.log( obj['name'] );` – reergymerej Aug 22 '15 at 04:21
  • See this fiddle: http://jsfiddle.net/83qsrg3g/ You need to use `object[key]`, but also pass the name of the key field as a string, `obj('name')`. Notice the quotes. – Matt Way Aug 22 '15 at 04:21
  • OK, I get it. For some reason that did not work in my Sublime console. Thanks. I thought I tried that syntax but clearly I need more practice. – mark jaramillo Aug 22 '15 at 04:28

0 Answers0