www.webdeveloper.com
Results 1 to 13 of 13

Thread: [RESOLVED] eval problem

  1. #1
    Join Date
    Nov 2009
    Posts
    3

    resolved [RESOLVED] eval problem

    Hi!
    I have a little problem with the eval function.

    Lets say I have this function to start with:
    Code:
    function foo() {
      var bar = function (a, b) {return a-b;};
      alert(bar(10,1));
    }
    This works quite all right, as a little warning pops up, saying 9.

    Next, I want the bar function to be specified by the user. The html page contains a textarea with id "method" and when the user clicks a button, the foo function executes.

    The user may have this in the textarea:
    function (a, b) {
    return a+b;
    }

    This is my foo function so far:
    Code:
    function foo() {
        var bar = eval(document.getElementById("method").value);
        alert(bar(10,1));
    }
    With this code, I get a syntax error at "function ^(a, b)..." (where ^ is at)

  2. #2
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    You're trying to eval() incomplete JavaScript. You either need to fully define the function (by providing a name), or strip the function "envelope" away completely:
    HTML Code:
    function (a, b) {
    return a+b;
    }
    to
    HTML Code:
    a+b
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  3. #3
    Join Date
    Apr 2006
    Location
    Houston
    Posts
    1,374
    Quote Originally Posted by liengen View Post
    The user may have this in the textarea:
    function (a, b) {
    return a+b;
    }

    This is my foo function so far:
    Code:
    function foo() {
        var bar = eval(document.getElementById("method").value);
        alert(bar(10,1));
    }
    With this code, I get a syntax error at "function ^(a, b)..." (where ^ is at)
    As said, give the function a name, then eval it. Something like this might work:
    Code:
    var strFunc = "newFuncName " + document.getElementById("method").value;
    var bar = eval(strFunc);
    alert(bar(10,1));

  4. #4
    Join Date
    Nov 2009
    Posts
    3
    Thanks. By typing just the function body works fine (a+b).
    Since this is an exercise, I would like to get it 100% correct :-)

    The exercise is something like this:
    I must sort some text (from the user) with the sort function in the Array object (myArray.sort(mySortFunc)). The mySortFunc is also specified by the user (the foo function in my first post). Well, this is working when my input is:
    Code:
    a.length - b.length;
    And my mySortFunc is:
    Code:
    return eval(document.getElementById("method").value);
    But, since the exercise clearly says that the input must be on the form:
    Code:
    function (a, b) {
        return a.length - b.length;
    }
    , this should be possible, even though I cannot see it, atleast not solely with eval (I can of course modify the string, removing everything except the body, but im sure thats not what my teacher is intending me to do).

  5. #5
    Join Date
    Apr 2006
    Location
    Houston
    Posts
    1,374
    I expect he wants you to use the prototype object to add custom methods to an existing object:

    Link to a quick example showing this: http://www.javascriptkit.com/javatutors/proto3.shtml

  6. #6
    Join Date
    Oct 2006
    Posts
    120
    Quote Originally Posted by slaughters View Post
    Link to a quick example showing this: http://www.javascriptkit.com/javatutors/proto3.shtml
    What a junk?!? That "tutorial" must be deleted, because give totally wrong explanations.

  7. #7
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    you don't need a name, or a prototype...

    function implies var, which without an assignment, eats the expression's intrinsic return.


    you have two options:
    Code:
    var str="function (a, b) {return a+b;}";
    
    var bar=eval("("+str+")");
    alert(bar(10,1));
    Code:
    var str="function (a, b) {return a+b;}";
    
    var bar; eval("bar="+str);
    alert(bar(10,1));
    Last edited by rnd me; 11-24-2009 at 03:32 PM.

  8. #8
    Join Date
    Apr 2006
    Location
    Houston
    Posts
    1,374
    Quote Originally Posted by forty2 View Post
    What a junk?!? That "tutorial" must be deleted, because give totally wrong explanations.
    Could be. The page I linked to looked rather straight forward and simple to me.

    Personally I dislike using the prototype object. I was never a fan of adding undocumented methods to existing objects even though I know that others think it is the greatest thing since sliced bread (after all it is the concept that all most all JavaScript frameworks are based on)

    BUT, to simply jump in and toss off that kind of one line comment in the middle of a thread:

    (a) Does not help the original poster at all
    (b) Does not make you look knowledgeable or helpful.
    Last edited by slaughters; 11-24-2009 at 03:25 PM.

  9. #9
    Join Date
    Nov 2009
    Posts
    3
    Thanks rnd_me, both suggestions worked like a charm.
    Really clever! :-D

  10. #10
    Join Date
    Oct 2006
    Posts
    120
    Quote Originally Posted by slaughters View Post
    Could be. The page I linked to looked rather straight forward and simple to me.
    Actually No. That "tutorial" is so much short. Author of the article doesn't explain in properly way prototyping mechanism. I'll be quote from article:

    simplifies the process of adding custom properties/ methods to all instances of an object.
    Actually `object' referred from `prototype' property of any object who have [[Construct]] method, will be put in the prototype chain. Here the properly term isn't is "adding" perhaps is "share". Object in ECMA inheritance runtime from prototype chain. See bellow:

    Code:
    function Foo(){};
    Foo.prototype = {
    	'arr' : []
    };
    
    var a = new Foo(),
    	b = new Foo();
    	
    a.arr.push(10, 20, 30); 
    b.arr.push(40, 50, 60);
    
    window.alert(Foo.prototype.arr); //10, 20, 30, 40, 50, 60
    window.alert(a.arr === b.arr);  //true 
    window.alert(a.hasOwnProperty('arr'));  //false
    window.alert(b.hasOwnProperty('arr'));  //false
    That is. Instances doesn't have copy of prototype chain. When `object' been created. That `object' will be refer to Constructor.prototype from internal property [[Prototype]]. So we can say, that `object' will be put in the front of prototype chain. All of that is missed from that tutorial. In ECMA 262-3 documentation all of that is explain in brilliant way.

    Quote Originally Posted by slaughters View Post
    Personally I dislike using the prototype object. I was never a fan of adding undocumented methods to existing objects even though I know that others think it is the greatest thing since sliced bread (after all it is the concept that all most all JavaScript frameworks are based on)
    Do you understand, how it works prototyping mechanism? If you don't understand, i strongly recommend you to read ECMA spec. Perhaps you talk about augmenting `object' referred from prototype property for Built-in objects? Like Array, Function, Object.
    There have one general problem. In ECMA implementation every property *have* internal attributes like {DontEnum}, {DontDelete}, {ReadOnly}. Any of these attributes specified different behavior.

    e.g. Variable declaration
    Code:
    (function(){
    	var propertyOfAO;
    	window.alert(delete propertyOfAO); //false
    }());
    Here, property of Activation/Variable Object with name `propertyOfAO' have internal attribute {DontDelete}. Behavior of `delete' operator is specified in: 11.4.1 ECMA 262 edition 3.

    When you augmenting object referred from `prototype' property. You make property who doesn't have internal attribute {DontEnum}. That reflected directly to For-in loop. Because For-in enumerate properties who doesn't have attribute {DontEnum} and For-in lookup in prototype chain.
    So, augmenting is harmful when it's is apply in up level of prototype chain.

    e.g.
    Code:
    Object.prototype.dummyProperty = true;
    var arr = [10, 20, 30];
    for (var i in arr)
    {
    	if (i === 'dummyProperty') 
    	{
    		window.alert(arr[i]); //true
    	}
    }

    Regards and i hope to understand my english

  11. #11
    Join Date
    Apr 2006
    Location
    Houston
    Posts
    1,374
    Using prototype to add methods to existing objects tend to create undocumented extensions to the language. Do this often enough and you end up either

    (a) With a set dense functions which are useful to the author, but hard to maintain by other developers later on (because they also tend to be uncommented and undocumented)

    or

    (b) Creating a derivative language.

  12. #12
    Join Date
    Oct 2006
    Posts
    120
    What exactly mean "uncommented" and "undocumented"? Do you use at all undocumented code? I'm not. If code isn't have good documentation, just don't use that code.

  13. #13
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    i tend to be a bit more lenient about prototypes, especially on string and array.
    i do think that is you add a potential native name, something like Array.prototoype.map, it should perform EXACTLY the same as the existing firefox/es5 version.

    jQuery drives me nuts because it's .map() reverses the order of the arguments compared to the aforementioned version. Prototype's (aka firebug) version doesn't pass the this argument as the 2nd argument to .map.

    Those kind of mixups confuse us all, and make debugging a bear.

    but afaic, help yourself to Array.prototype.map2()...

    if you stay out of my way, i don't need your documentation.
    Last edited by rnd me; 11-27-2009 at 12:27 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles