2

I am new to javascript and I'm trying some different things. Take this following example:

function obj(){
    this.execute = function(codeToExecute){
        codeToExecute();
    }
}

// Object init and function call

var obj = obj();
obj.execute(function(){
    alert("G'Day!");
}

This will execute the alert message. All good until now but now I'm trying to alert a message transmitted through a parameter:

var obj = obj();
obj.execute(function(message){
    alert(message);
}

What should be the structure of the function obj() now that I have to insert that parameter somewhere?

I couldn't find anything useful on google because honestly I don't know exactly what I should be looking for. Thank you!

3 Answers 3

4

You can extend execute so that any additional parameters are passed to the supplied function:

function Obj() {
    this.execute = function(f) {
        var args = [].slice.call(arguments, 1);
        f.apply(this, args);
    }
}

var obj = new Obj();
obj.execute(function(message){
    alert(message);
}, "boo!");

This line is the "magic" one:

var args = [].slice.call(arguments, 1);

It uses the Array.prototype.slice function which is used to copy arrays, but (kind of) tricks the function into using the arguments pseudo-array as the source array (instead of the supplied []), copying all of the elements apart from the first.

You can't just use arguments.slice(1) because arguments isn't a real JS array. It has a .length property, and you can access arguments[n], but it doesn't have all of the extra functions in its prototype that a real array has. It's close enough though that the implementation of .slice() doesn't know any better.

NB: you should use new to create an object instance - in your original code you're just calling obj() immediately and then reassigning the (undefined) result back to obj - that code could never have worked at all.

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

3 Comments

+1 though the OP might need an example of how to call it from his code :)
Deleted my answer. This is the answer.
@Alnitak Thank you! It works. Could you please explain [].slice.call(arguments, 1)? I found some details about call() and apply() but nothing about []. Thanks again!.
2

You can do it like this:

function obj(){
 this.execute = function(codeToExecute, arg){
     codeToExecute(arg);
   }
} 

var obj = new Obj(); 
obj.execute(function(message){
               alert(message);},
           "yourmessage");

Essentially, you pass 2 arguments to your obj.execute: the function, and the argument to that function.

2 Comments

For someone new to JavaScript, this might be a little easier to understand. Using apply and using [] to get the argument array is a little more advanced, even though it is more general purpose.
Someone new to js should not get into these bad habits. Make maintenance difficult
1

You could use the good old set/get methods. Below I'm passing in the message into the object constructor and it can be accessed with the getMessage function. You can include a set function if you don't want to pass into the constructor

function Obj(message){
this.getMessage = function(){
return message;
}
    this.execute = function(codeToExecute){
        codeToExecute();
    }
}

var obj = new Obj("hello");
obj.execute(function(){
    alert(obj.getMessage());
});

1 Comment

that's really horrible. It makes the function being passed unnecessarily dependent on Obj.

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.