4

How to get an object from a closure, that's confusion with me, here is the question:

var o = function () {
   var person = {
       name: 'jonathan',
       age: 24
   }
   return {
       run: function (key) {
           return person[key]
       }
   } 
}

question: How do i get original person object without changing the source code.

4

5 Answers 5

10

var o = function() {
  var person = {
    name: 'jonathan',
    age: 24
  }
  return {
    run: function(key) {
      return person[key]
    }
  }
}

Object.defineProperty(Object.prototype, "self", {
  get() {
    return this;
  }
});

console.log(o().run("self")); // logs the object

This works as all objects inherit the Object.prototype, therefore you can insert a getter to it, which has access to the object through this, then you can use the exposed run method to execute that getter.

11
  • Seems I'm good at interview questions :) Feb 21, 2019 at 13:37
  • 2
    @adiga it depends how you "source code". If you just mean "don't change the code that defines the closure" i.e., what's quoted in the OP, then defining a getter works. If it means without changing ANY code, then it's not possible by definition because you are not allowed to change anything at all.
    – VLAZ
    Feb 21, 2019 at 13:41
  • 1
    I'd use a Symbol instead of "self" so I don't break any object which could have "self" (or any other key) defined.
    – Dmitry
    Feb 21, 2019 at 13:45
  • (@VLAZ I wasn't being serious)
    – adiga
    Feb 21, 2019 at 13:45
  • @dmitry I wouldn't use this code at all. In reality no one would have a function o that returns keys of a person? This is constructed code meant to challenge interviewies. Feb 21, 2019 at 13:48
2

You can get the keys by running

o().run("<keyname>"))

Like that:

var o = function () {
   var person = {
       name: 'jonathan',
       age: 24
   }
   return {
       run: function (key) {
           return person[key]
       }
   } 
}

console.log(o().run("name"));
console.log(o().run("age"));

11
  • That's meaningless if you don't know the property names. Feb 21, 2019 at 13:28
  • @NisargShah I can look at the code and see the property names, or maybe they are documented... lol Feb 21, 2019 at 13:29
  • @NisargShah As far as I know it would be impossible to return the whole person object without changing the code. And since OP said that was an interview question, I believe this is the way he was supposed to do it.
    – NullDev
    Feb 21, 2019 at 13:30
  • @epascarello I'm pretty sure that the interviewer is not expecting that answer. :) The expected answer would be "You can't". Feb 21, 2019 at 13:30
  • 2
    All three answers "can't", "read props", and "define/eval" all show the basic concept that they know that the person is not accessible directly. For no, I would follow up with another answer on how can you "recreate" with the available method. For the method one, I would have followed up with why can't you get "person" directly just so I hear the words I think they know. And for the other one.... that guy is crazy. Feb 21, 2019 at 13:51
1

Could just toString the function, pull out the part you need, and eval it to get it as an object. This is pretty fragile though so getting it to work for different cases could be tough.

var o = function () {
   var person = {
       name: 'jonathan',
       age: 24
   }
   return {
       run: function (key) {
           return person[key]
       }
   } 
}

var person = eval('(' + o.toString().substr(30, 46) + ')')

console.log(person)

5
  • 3
    Disclaimer, this is a super ugly hack and using it in production code is not advisable. It might have adverse effect on your health. From your coworkers hitting you.
    – VLAZ
    Feb 21, 2019 at 13:34
  • I mean it's terrible but shows you can think outside the box. Definitely not something I'd use in production. :P Feb 21, 2019 at 13:37
  • To be fair, this interview question is pretty bad. So, as they say - ask stupid questions, get stupid answers.
    – VLAZ
    Feb 21, 2019 at 13:39
  • Exactly. In what situation would you need to get this without being able to change the source code? You should never need to access something that is in a closure from outside of it. Feb 21, 2019 at 13:41
  • you know what - I actually thought of a good reason to. Well, not the best, to be honest - it's still relatively bad all things considered but here it goes - testing. If you want to make a spy for unit testing purpose you might need to "break into" a closure. It's still better to 1. use a dedicated library 2. write unit-testable code, however, this is still a more legitimate reason I can think of. Everything else is related to bad architecture, bad design, bad coding, or multiple of these.
    – VLAZ
    Feb 21, 2019 at 14:37
-2

o().run("name") It will be return "jonathan".

-2

Simply you can make this

<script type="text/javascript">
 var o = function () {
  var person = {
   name: 'jonathan',
   age: 24
  }
  return {
   run: function (key) {
       return person[key]
   }
  } 
 }
let a = new o;
alert(a.run('name'));
</script>

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.