Javascript: Debuging code created by eval() and new Function()

im trying for put a private var into an already existent function, example:

var AObject={ get:function(s){ return s.toLowerCase()+a; } } function temp(){ var a="A"; var o={}; eval("o.get="+AObject.get.toString()); reurn o; } var bObject=temp(); BObject.get("B"); // "bA" BObject.get(); /* error: s is undefined; but marked in line "return o;" and not in "return s.toLowerCase()+a;"*/

My target is to run get() function, owned by an existent AObject, with private var ... Im obtain it using eval(or new Function), but unfortunly the debugger will be broke!

So, there is a way to achieve this without using eval, or a way to use eval and keep debugger useful?


The reason the debugger doesn't point to the line in the function, is that the function you are calling is not related at all (as far as JavaScript is concerned) to the AObject.get function. Eval has no way of knowing where the string defining the function came from. The debugger should be pointing to the line where you call eval, because that's where the function is defined, but it's apparently off by a line.

To answer your question, I don't think there's a way to avoid eval (or Function, which would probably be preferable) unless you can move the function definition inside temp so it closes over "a" or add an "a" parameter to the get function.

I think using an eval is your only option, if you want the local variable a to be usable inside the cloned function. Also, I would try a different debugger (Firebug on Firefox?)

function objectbuilder(a){
return {
return s.toLowerCase() + a;

function temp(){
return objectBuilder("A");

var bObject=temp();
BObject.get("B"); // "bA"


function temp(obj){
var a="A";
var o={};
reurn o;

var myObj=temp({
return s.toLowerCase()+a;

myObject.get("B"); //bA

Are you understand now?


No, your way works only becouse you put variable in window scope (global...)

Your "this" key is the "window" object, so when you do


is like do


In this way become a global var

function temp(obj){
var a="A";

return (function(){
this.a =a;
var o={};
return o;


var myObject=temp({
return s.toLowerCase()+a;

alert(myObject.get("B")); //bA
alert(a); //A

This is not a workaround, must be private, not global. If i decide to use global var im starting to use a global vector where i can store my private var. Thank you.

Are you trying to do something like this???

var AObject={
return s.toLowerCase()+this.a;

function temp(){

return {get:function(s){return{a:"A"},s);}};


EDIT: after his comment;

do you know, what you would do is pretty wrong, it works only because eval change the scope chain, basically you're doing this:

function temp(obj){
var a="A";
var o={};

return s.toLowerCase()+a;

return o;

since a is in a closure, this will work, but you're not calling the true obj get function, instead you're creating another one that work inside temp;

But, as I state in the comment, you shouldn't write function that contains undeclared variable, instead you should pass this variable at the function, as svinto does.

Edit: deleted wrong code.

Category:javascript Time:2009-09-28 Views:0

Related post

  • Shorten 3 lines of code creating an object with a function? 2011-08-06

    I'm trying to make these three lines this.door = create("img"); this.door.src = "img/door.png"; = "absolute"; be easily repeated with one line like this, using a function this.door = putimg("img/door.png"); since the only thi

  • If I changing a Javascript native code will it affect the jQuery functions? 2011-04-10

    I want to prevent users from inserting javascript commands in the url to prevent them from changing hidden input field values and submitting it to the database. So if I change some native functions at the end of each page for example. document.getEle

  • Access variable from code run in "eval" in Javascript 2012-03-23

    I know using eval is not at all recommended and I have read this link too. Set Variable inside Eval (JavaScript) However, this is what I want to do. Lets say we have some code in a textbox. So I have to take that text, and then find out all the globa

  • Accessing Elements created by Javascript- In code behind in MVC 3 2011-05-25

    In my cshtml i have defined a tag I am creating a Image Upload Form, where On File Submit, i am saving the value in a Temp Table(SQL) and returning(GUID) the value thru a Ajax call and Binding the GUID inside Dynamically created DIV Tag(Jquery/Javasc

  • How can I get html code created by javascript? 2011-06-13

    I need to get html code of a web site using c# (web browser or webrequest) but I have a problem for html code created by javascript. actually I need View Generated Source of Web Developer plugin (its plugin of firefox) how can i do it? edit1: I found

  • How to create rails restful member routes in javascript/jQuery code? 2011-08-11

    How to create rails restful member routes in javascript/jQuery code ? Here is scenario where i wanted to add customer javascript variable to rails route :javascript data += "<li><a href=\"#{edit_customer_path('""')}\">Edit

  • Why is my JavaScript debug window in QT (webview) not showing my code? 2014-07-17

    This is a fairly complex application. I find sometimes when I make changes to the code that causes a JS error, the error is not logged and on some platforms it actually causes the entire JavaScript debug inspector in webkit to be blank. This happens

  • How to create a pause in javascript/jquery code that follows a call to asynchronous function 2011-05-18

    How can I call an asynchronous function and then create a pause in the code that follows the call to the function so that I may be (almost) sure that the asynchronos function has finished? I don't want to put the code that follows the call inside a f

  • How do you keep debug code out of production? 2010-12-09

    It happens to the best of us. Particularly when dealing with languages without built in debugging capabilities such as breakpoints and watched variables, these bugs bite developers. Debugging code, alerts and Response.Writes, show up in production co

  • Javascript: Better Code / Better Design help 2011-02-22

    I've been trying to read and understand a bit more about better code and design with Javascript, and really the lack of tutorials explicitly describing about the benifts / uses of creating code in a different way than what a majority of the online co

  • JavaScript Debugging on Android/iPhone 2012-01-24

    I have an strange problem with sencha touch. I have an application, where there are Select fields, And on change of select field value I am applying some rules, as per my business rules. Rules are Rules is like i am removing all of the field elements

  • Javascript AJAX include file witth eval 2012-01-26

    Suppose I have 1) a HTML document. 2) This HTML document loads Javascript file "code.js" like this: <script src="code.js"> 3) User clicks button which runs "fetchdata" function in "code.js", 4) "fetchdata" function looks like this: var xmlhttp

  • What's the difference between void, eval, and the Function constructor in JavaScript? 2012-04-27

    What's the processing model for executing code within these different statements? void(alert('hi')) undefined eval(alert('hi')) undefined Function(alert('hi')) function anonymous() { undefined } eval(Function(function foo(){return true}).toString())(

  • Cross-browser JavaScript debugging 2008-09-17

    I have a few scripts on a site I recently started maintaining. I get those Object Not Found errors in IE6 (which Firefox fails to report in its Error Console?). What's the best way to debug these- any good cross-browser-compatible IDEs, or javascript

  • Hardest javascript debugging problem ever 2008-10-13

    We have an ASP.NET application and when running on client site they often get null reference Javascript errors. As with all these errors the information IE6 displays is less than helpful. But the problem is as soon as I install IE script debugger and

  • Is there any good JavaScript hash(code/table) implementation out there? 2008-10-22

    Yes, I know you could use regular objects as associative arrays in JavaScript, but I'd like to use something closer to java's Map's implementation (HashMap, LinkedHashMap etc). Something that could have any kind of data used as key. Are there any goo

  • How do you Manage Infragistics WebGrid Data from JavaScript/Ajax code? 2008-10-23

    I am working on a project that I need to use the Infragistics WebGrid control for some lists of data. I am loading the data on the client-side using JavaScript for display on a Map, and then I need to display that same data within multiple WebGrids.

  • How to debug code that uses boost w/o losing sanity? 2008-11-18

    Boost is a great set of libraries and it really boosts productivity. But debugging code that uses it is a total nightmare. Sure, stepping through twenty thousand header files can be a valuable intellectual exercise, but what if you need to do it over

  • Visual Studio 2005 Javascript debugging 2008-11-28

    Is it possible to stop visual studio from debugging external javascript? We have a window which displays external webpage. If the page contains bad javascript then my entire session comes to a halt and breaks. Hitting continue won't move past the pro

Copyright (C), All Rights Reserved.

processed in 0.966 (s). 13 q(s)