0

I have an object method a like below:

var f = {
   a: function(e){
       e.call();
       console.log(t);    // Here should print “Hello world”
   }
};
f.a(function(){
    f.a.t = "Hello world";
    // How to pass the string “Hello world” into the object method “a” ?
});

e is an anonymous function. I call the e and now I want to pass a string Hello world into the object method a. If it's not allowed to use global variable, how can I pass the string into the object method?

6 Answers 6

1

You may want to consider to change the return value of your eas below:

    var f = {
       a: function(e){
           var t = e.call();//here declare variable t
           console.log(t);    // Here should print “Hello world”
       }
    };
    f.a(function(){
        return "Hello world";//directly return string value from anonymous function
        // How to pass the string “Hello world” into the object method “a” ?
    });

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

1 Comment

Very elegant. This solution nicely decouples the declaration of f and the anonymous function: you don't have to know the inner working of f in the anonymous function. In the other solutions -including the one I proposed- the anonymous function has to know that, in order to pass the string "Hello world", it has to set the value of the attribute t of object f or this (in which case, if you forget to add this to e.call(), it doesn't work).
0

If you want to call e in the context of f then you would need to pass f to call, writing e.call() would be equal to e().

Beside that t refers to a variable and not to the property t of a. You cannot set a variable that way, but you could store it in the object f

You would write it that way.

var f = {
   a: function(e){
       e.call(this);
       console.log(this.t);
   }
};
f.a(function(){
    this.t = "Hello world";
});

Comments

0

See below snippet

var f = {
   a: function(e){
       e.call();
       console.log(f.a.t);    // Here should print “Hello world”
   }
};
f.a(function(){
    f.a.t = "Hello world";
    // How to pass the string “Hello world” into the object method “a” ?
});

Comments

0

What is t's scope? If it's an attribute of f, write this:

var f = {
    a: function(e){
        e.call();
        console.log(this.t); // this line changed
    }
};
f.a(function(){
    f.t = "Hello world"; // this line changed
});

Comments

0

Why not define a property in object like:

var f = {
  t: '',
  a: function(e){
    e.call();
    console.log(this.t);    // Here should print “Hello world”
  }
};
f.a(function(){
    f.t = "Hello world";
});

Comments

0

f is java script object and you can add property to that.I just added f.t="Hello world".you can use f.t anywhere in programme wherever you have f scope.

var f = {
       a: function(e){
           e.call();
           console.log(f.t);    // Here should print “Hello world”
       }
    };
    f.a(function(){
        f.t = "Hello world";
        // How to pass the string “Hello world” into the object method “a” ?
    });

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.