0

Here is something weird, and I don't know how to overcome it.

Basically, I have this string here:

var s = 'var Bla = {variable: true, get: function() { return this.variable;}}; return Bla;'

and using the Function constructor to parse this construct.

var fn = new Function(s);

It works great, except that the inside function 'get' is no longer there .. like it didn't got parsed?!

fn().variable  -> is there and returns "true".
fn().get  -> is undefined, not existing.

Any ideas?

PS: I edited my original question to indicate what's really missing.

7
  • Because you aren't passing a function to the function constructor. Commented Nov 20, 2014 at 21:52
  • I wonder if you are running into the getter syntax. You could try changing the name of that property. Commented Nov 20, 2014 at 21:52
  • 2
    @Brennan - they're passing a function body to the constructor which is allowed. Commented Nov 20, 2014 at 21:53
  • Ah, thanks, was not aware of that Commented Nov 20, 2014 at 21:55
  • Btw - eval the same issue. @Brennan: new Function is the function constructor. Check developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Nov 20, 2014 at 21:56

2 Answers 2

4

The code you've posted is the equivalent of this:

var fn = function() {
    var Bla = {
        variable: true, 
        get: function() { 
            return this.variable;
        }
    };
    return Bla;
}

So you can't directly use fn.variable or fn.get - instead you need to call that function:

var x = fn();
alert(x.get()); //alerts true
x.variable = 1;
alert(x.get()); //alerts 1

Bla is just a local variable within the function, and doesn't get returned from it. The code could easily just have been:

var fn = function() {
    return {
        variable: true, 
        get: function() { 
            return this.variable;
        }
    };
}

And you'd get the same effect. After calling the function, the object returned from it is what contains variable and get:

example object

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry I should have been more explicit. This works, however the .get is not part of the return object of Bla .. it's just not there.
@flyandi Can you explain more? Bla is just a local variable within the function and effectively ceases to exist as soon as the function returns. The object returned from the function certainly does contain both variable and get: imgur.com/VLm4b7y
I feel stupid right now .. I literally forgot the () on one of the callers of this routine within my library.. but it was the missing () that caused the issue :-) - Thanks everyone!
0

Your example works fine.

I tried it and issuing:

fn().get()

returns true as you'd expect.

4 Comments

@flyandi—any browser that supports ES5 (such as Chrome).
I did it in chrome. I opened the debugger and copy and pasted the 2 lines you have into the console. Then I issued: fn().get();
also fn().bla returns undefined as i'd expect. Your first post (edited I see) says you got something different? That doesn't make any sense. fn() will return the Bla object. That object has no property "bla".
Yeah I figured it out, I was missing the () from one of the sub modules which is using the raw constructor :-) .. I modified my post to reflect the correct property ("variable").

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.