Mark
Mark

Reputation: 18

Javascript/JQuery: Assign a whole line of code to a function parameter?

Is it possible to assign a whole line of code to a function parameter? Take this example:

function testFunc(parameter1){
    parameter1;
}

testFunc($(".someClass").text("someText"));

When the function is used with that parameter, can the parameter1 be replaced by the line of code?

I'm new with JavaScript and jQuery, so I'm just curious if this is possible. I did not see any questions like this asked before. But if it was asked, a link to the question would be appreciated. Thanks

Upvotes: 0

Views: 67

Answers (5)

Koti Panga
Koti Panga

Reputation: 3720

Yes you can pass function callback like regular primitive variable

In your case you should check param type before execution

function testFunc(parameter1){

    if(typeof parameter1==="undefined"){
       //arguments[0] will fall here
       console.log("No arguments case. parameter1 not defined")
    }
    else //function check 
    if(typeof parameter1==="function"){
       //you can parameter function here.
        return parameter1();
    }
    else{
        //regular case value or object, other than function types fall here
        console.log("not a function, received param type: "+ typeof(parameter1));
        return parameter1;
    }

}

$(function (){
    //let us say you have below vars
    var primitiveVar="test",
        fun = function(){console.log("function fun call")};

    //no args here
    testFunc();

    //sending primitiveVar
    testFunc(primitiveVar);

    //below is your call with jQuery Obj
    testFunc($(".someClass").text("someText"));
});

Upvotes: 0

plalx
plalx

Reputation: 43728

Arguments aren't assigned to functions, they are passed/sent/[insert other synonym here].

Anything that is an expression (any code which evaluates to some value) can be passed around.

In your exemple $(".someClass").text("someText") is an expression which evaluates to a jQuery object, so you can use this unit of code as a function's argument without any doubt.

However, if you want to pass around some code which has to be executed as part of an existing function's process, you must use a function expression which encapsulates that behavior.

E.g.

function executor(task) {
    task();
}


executor(function () {
    //code to be executed by the executor
});

Upvotes: 0

Drew Noakes
Drew Noakes

Reputation: 311255

The key insight here is that a function can be treated like any other variable.

For example:

var i = 1;
var f = function() { console.log('hello!'); };

Here f is a value, just like i is, but you can invoke it just like any other function:

f(); // prints 'hello!' in the console

Because it is a value, you can pass it to another function:

function g(h) { h(); }

g(f); // prints 'hello!' in the console

Take the time to ensure you understand the above code. I've deliberately used vague names so you can learn the mechanics. Let me know if you have any questions.

Upvotes: 0

invinciblejai
invinciblejai

Reputation: 1293

Yes it can be done , as jQuery will eveluate it and return a object.

Upvotes: 0

iCollect.it Ltd
iCollect.it Ltd

Reputation: 93601

Sounds like you are inventing the callback :)

Pass an actual function and call it with ();

function testFunc(callback){
    callback();
}

testFunc(function(){$(".someClass").text("someText");});

Upvotes: 1

Related Questions