Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

You cannot. bar is called before the object is constructed, obj will be undefined and the already-evaluated values of the previous properties (1 and 2) are somewhere inaccessible in memory only. See also Self-references in object literals / initializersSelf-references in object literals / initializers.

Given you found this question in a quiz with pretty arbitrary restrictions, they seem to expect a trick answer. There are several ways:

  • Access to source code and evaluate the object literal yourself

  • Simply return a constant, given that obj.x and obj.y are constant in the given code as well

  • Overwrite console.log to do your bidding, for example

      function bar() {
          var log = console.log.bind(console);
          console.log = function(p) {
              log(p.valueOf());
          };
          return {
              valueOf: function() {
                  return obj.x + obj.y;
              }
          };
      }
    

    Doesn't work unfortunately due to console.log being dereferenced before foo() is called.
    A similar approach does work in environments where the console.log behaviour can be customized without needing to overwrite anything:

      function bar() {
          return {
              inspect: function() {
                  return String(obj.x + obj.y);
              }
          };
      }
    
  • Just call foo() yourself to get the values, but don't recurse infinitely on bar:

      function bar() {
          if (foo.stop) return null;
          foo.stop = true;
          var res = foo().x + foo().y;
          foo.stop = false;
          return res;
      }
    

You cannot. bar is called before the object is constructed, obj will be undefined and the already-evaluated values of the previous properties (1 and 2) are somewhere inaccessible in memory only. See also Self-references in object literals / initializers.

Given you found this question in a quiz with pretty arbitrary restrictions, they seem to expect a trick answer. There are several ways:

  • Access to source code and evaluate the object literal yourself

  • Simply return a constant, given that obj.x and obj.y are constant in the given code as well

  • Overwrite console.log to do your bidding, for example

      function bar() {
          var log = console.log.bind(console);
          console.log = function(p) {
              log(p.valueOf());
          };
          return {
              valueOf: function() {
                  return obj.x + obj.y;
              }
          };
      }
    

    Doesn't work unfortunately due to console.log being dereferenced before foo() is called.
    A similar approach does work in environments where the console.log behaviour can be customized without needing to overwrite anything:

      function bar() {
          return {
              inspect: function() {
                  return String(obj.x + obj.y);
              }
          };
      }
    
  • Just call foo() yourself to get the values, but don't recurse infinitely on bar:

      function bar() {
          if (foo.stop) return null;
          foo.stop = true;
          var res = foo().x + foo().y;
          foo.stop = false;
          return res;
      }
    

You cannot. bar is called before the object is constructed, obj will be undefined and the already-evaluated values of the previous properties (1 and 2) are somewhere inaccessible in memory only. See also Self-references in object literals / initializers.

Given you found this question in a quiz with pretty arbitrary restrictions, they seem to expect a trick answer. There are several ways:

  • Access to source code and evaluate the object literal yourself

  • Simply return a constant, given that obj.x and obj.y are constant in the given code as well

  • Overwrite console.log to do your bidding, for example

      function bar() {
          var log = console.log.bind(console);
          console.log = function(p) {
              log(p.valueOf());
          };
          return {
              valueOf: function() {
                  return obj.x + obj.y;
              }
          };
      }
    

    Doesn't work unfortunately due to console.log being dereferenced before foo() is called.
    A similar approach does work in environments where the console.log behaviour can be customized without needing to overwrite anything:

      function bar() {
          return {
              inspect: function() {
                  return String(obj.x + obj.y);
              }
          };
      }
    
  • Just call foo() yourself to get the values, but don't recurse infinitely on bar:

      function bar() {
          if (foo.stop) return null;
          foo.stop = true;
          var res = foo().x + foo().y;
          foo.stop = false;
          return res;
      }
    
added 405 characters in body
Source Link
Bergi
  • 670.4k
  • 162
  • 1k
  • 1.5k

You cannot. bar is called before the object is constructed, obj will be undefined and the already-evaluated values of the previous properties (1 and 2) are somewhere inaccessible in memory only. See also Self-references in object literals / initializers.

Given you found this question in a quiz with pretty arbitrary restrictions, they seem to expect a trick answer. There are several ways:

  • Access to source code and evaluate the object literal yourself

  • Simply return a constant, given that obj.x and obj.y are constant in the given code as well

  • Overwrite console.log to do your bidding, for example

      function bar() {
          var log = console.log.bind(console);
          console.log = function(p) {
              log(p.valueOf());
          };
          return {
              valueOf: function() {
                  return obj.x + obj.y;
              }
          };
      }
    

    Doesn't work unfortunately due to console.log being dereferenced before foo() is called.
    A similar approach does work in environments where the console.log behaviour can be customized without needing to overwrite anything:

      function bar() {
          return {
              inspect: function() {
                  return String(obj.x + obj.y);
              }
          };
      }
    
  • Just call foo() yourself to get the values, but don't recurse infinitely on bar:

      function bar() {
          if (foo.stop) return null;
          foo.stop = true;
          var res = foo().x + foo().y;
          foo.stop = false;
          return res;
      }
    

You cannot. bar is called before the object is constructed, obj will be undefined and the already-evaluated values of the previous properties (1 and 2) are somewhere inaccessible in memory only. See also Self-references in object literals / initializers.

Given you found this question in a quiz with pretty arbitrary restrictions, they seem to expect a trick answer. There are several ways:

  • Access to source code and evaluate the object literal yourself

  • Simply return a constant, given that obj.x and obj.y are constant in the given code as well

  • Overwrite console.log to do your bidding, for example

      function bar() {
          var log = console.log.bind(console);
          console.log = function(p) {
              log(p.valueOf());
          };
          return {
              valueOf: function() {
                  return obj.x + obj.y;
              }
          };
      }
    

    Doesn't work unfortunately due to console.log being dereferenced before foo() is called.

  • Just call foo() yourself to get the values, but don't recurse infinitely on bar:

      function bar() {
          if (foo.stop) return null;
          foo.stop = true;
          var res = foo().x + foo().y;
          foo.stop = false;
          return res;
      }
    

You cannot. bar is called before the object is constructed, obj will be undefined and the already-evaluated values of the previous properties (1 and 2) are somewhere inaccessible in memory only. See also Self-references in object literals / initializers.

Given you found this question in a quiz with pretty arbitrary restrictions, they seem to expect a trick answer. There are several ways:

  • Access to source code and evaluate the object literal yourself

  • Simply return a constant, given that obj.x and obj.y are constant in the given code as well

  • Overwrite console.log to do your bidding, for example

      function bar() {
          var log = console.log.bind(console);
          console.log = function(p) {
              log(p.valueOf());
          };
          return {
              valueOf: function() {
                  return obj.x + obj.y;
              }
          };
      }
    

    Doesn't work unfortunately due to console.log being dereferenced before foo() is called.
    A similar approach does work in environments where the console.log behaviour can be customized without needing to overwrite anything:

      function bar() {
          return {
              inspect: function() {
                  return String(obj.x + obj.y);
              }
          };
      }
    
  • Just call foo() yourself to get the values, but don't recurse infinitely on bar:

      function bar() {
          if (foo.stop) return null;
          foo.stop = true;
          var res = foo().x + foo().y;
          foo.stop = false;
          return res;
      }
    
added 405 characters in body
Source Link
Bergi
  • 670.4k
  • 162
  • 1k
  • 1.5k

You cannot. bar is called before the object is constructed, obj will be undefined and the already-evaluated values of the previous properties (1 and 2) are somewhere inaccessible in memory only. See also Self-references in object literals / initializers.

Given you found this question in a quiz with pretty arbitrary restrictions, they seem to expect a trick answer. There are several ways:

  • Access to source code and evaluate the object literal yourself

  • Simply return a constant, given that obj.x and obj.y are constant in the given code as well

  • OverwriteOverwrite console.log to do your bidding, for example

      function bar() {
          var log = console.log.bind(console);
          console.log = function(p) {
              log(p.valueOf());
          };
          return {
              valueOf: function() {
                  return obj.x + obj.y;
              }
          };
      }
    

    Doesn't work unfortunately due to console.log being dereferenced before foo() is called.

  • Just call console.logfoo() yourself to do your biddingget the values, for examplebut don't recurse infinitely on bar:

      function bar() {
          var log =if console.log.bind(console);
          consolefoo.log = function(pstop) {
             return log(p.valueOf());null;
          };
         foo.stop return= {true;
            var res valueOf:= functionfoo() {
                  return obj.x + objfoo().y;
            foo.stop = }false;
          }return res;
      }
    

You cannot. bar is called before the object is constructed, obj will be undefined and the already-evaluated values of the previous properties (1 and 2) are somewhere inaccessible in memory only. See also Self-references in object literals / initializers.

Given you found this question in a quiz with pretty arbitrary restrictions, they seem to expect a trick answer. There are several ways:

  • Access to source code and evaluate the object literal yourself

  • Simply return a constant, given that obj.x and obj.y are constant in the given code as well

  • Overwrite console.log to do your bidding, for example

      function bar() {
          var log = console.log.bind(console);
          console.log = function(p) {
              log(p.valueOf());
          };
          return {
              valueOf: function() {
                  return obj.x + obj.y;
              }
          }
      }
    

You cannot. bar is called before the object is constructed, obj will be undefined and the already-evaluated values of the previous properties (1 and 2) are somewhere inaccessible in memory only. See also Self-references in object literals / initializers.

Given you found this question in a quiz with pretty arbitrary restrictions, they seem to expect a trick answer. There are several ways:

  • Access to source code and evaluate the object literal yourself

  • Simply return a constant, given that obj.x and obj.y are constant in the given code as well

  • Overwrite console.log to do your bidding, for example

      function bar() {
          var log = console.log.bind(console);
          console.log = function(p) {
              log(p.valueOf());
          };
          return {
              valueOf: function() {
                  return obj.x + obj.y;
              }
          };
      }
    

    Doesn't work unfortunately due to console.log being dereferenced before foo() is called.

  • Just call foo() yourself to get the values, but don't recurse infinitely on bar:

      function bar() {
          if (foo.stop) return null;
          foo.stop = true;
          var res = foo().x + foo().y;
          foo.stop = false;
          return res;
      }
    
Source Link
Bergi
  • 670.4k
  • 162
  • 1k
  • 1.5k
Loading