0

I need to execute the fc.text-function via fc.messages.max.call( this );

But I do have the error: TypeError: fc is undefined.

The pure fc.text-function works.

I need some explanation with the scopehandling.

Here is the Fiddle

Here is my Code

var fc = {

        methods: { },

        test: function(whatever) {
              console.log("test:", whatever);
            },
        messages: {
            tata: "hello", 
            max: fc.test("Geben Sie bitte einen Wert kleiner oder gleich {0} ein."),
            min: fc.test("Geben Sie bitte einen Wert größer oder gleich {0} ein."),
        },

        otherTest: function() {
                console.log("otherTest works"); 
                fc.test("otherTest");     
                }

   };

fc.test("hello world"); //works
fc.otherTest();         //works also

//fc.messages.max.call( this );

How can i fix this ?

I made another jsfiddle where it works fine. but the function is in a different scope. one more fiddle

Any explanation?

2
  • What do you want messages.max and messages.min to become? Commented Apr 7, 2014 at 12:04
  • This has nothing to do with the this context, you don't need to use .call Commented Apr 7, 2014 at 12:05

3 Answers 3

1

The call() method is a property of of the Function object.

fc.messages.max isn't a function. It's value is the return value of fc.test(), which is undefined.

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

2 Comments

If you have a look to my second jsfiddle, there it works. the only different is that the function is in an other scope. Why I can't call them like i did?
You can't access fc from within fc. When you declare max: fc.test( fc wont exist yet, as JS need to compile the entire object before it sets the fc variable.
1

From the mdn docs:

The call() method calls a function with a given this value and arguments provided individually.

min: & max: were not functions so you could not call() them.

What calls what:

var test = function( t ) {
    console.log("one more test: ",t);
};
var fc = {
    
    methods: { },
    
    test: function(whatever) {
        console.log("test:", whatever);
    },
    messages: {
        tata: "hello", 
        max: function() { test("var test") },
        min: function() { fc.test("fc test") }
    },
    
    otherTest: function() {
        console.log("otherTest works"); 
        fc.test("otherTest");     
    }
    
};

fc.test("fc test, hello world"); //calls fc.test
test("var test, hello world"); // calls var test
// fc.otherTest();         //works also

fc.messages.max.call( this ); // call var test
fc.messages.min.call( this ); // calls fc.test

http://jsfiddle.net/v7xT6/7/

5 Comments

thx but this is not what iam looking for. I made on your base another fiddle to demonstrate jsfiddle.net/v7xT6/3. this works fine. but the function is in another scope.
@hamburger jsfiddle.net/v7xT6/5 ? this calls the var test function. Which test function do you want to call the one inside fc, or the one outside?
@hamburger i've updated the answer to show what calls what heres the fiddle: jsfiddle.net/v7xT6/7
thx Rick, I have seen it. In my second jsfiddle it works without declaring max as a function. I'am looking for an explanation, because I do not understand why?
@hamburger in your second fiddle you are not calling the max function, when you do call it it throws an undefined error, because its not a function.
0

Im not sure, but i think you can't access fc.test() because in messages fc is not complete yet.

compare:

var f = { a: function () { return 1; }, max: f.a() }
> TypeError: Cannot call method 'a' of undefined

It says f is undefined.

You can't access f until the assignment (var f) is complete and f is defined.

Comments

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.