0

I am creating a new object variable and passing an object as an argument:

var obj = new test({
    first : $('#fname').val(),
    last : $('#lname').val(),
    fn : function() {
        alert(this.first + " " + this.last);
    }
});

This is the called function when creating the above variable:

var test = function(obj) {
    this.fn = function() {
        alert("No custom function was made.");
    }

    this.first = obj.first;
    this.last = obj.last;
    if(obj.fn)
        this.fn = obj.fn(); //I also tried it without the '()' after 'obj.fn'
};

The first and last variables are fine, but I cannot figure out how to get the custom function that is passed to be set.

2 Answers 2

2

You mentioned that you tried removing the '()' after 'obj.fn'... seems to work when I do that. Here's the link to verify: http://jsfiddle.net/TD93x/3/

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

1 Comment

mhmm wow ok idk why but it worked. i already tried that a bunch of times..... -_- thanks!
2

it worked for me

var test = function(obj) {
    this.fn = function() {
        alert("No custom function was made.");
    }

    this.first = obj.first;
    this.last = obj.last;
    if(obj.fn)
        this.fn = obj.fn;
};

1 Comment

mhmm wow ok idk why but it worked. i already tried that a bunch of times..... -_- thanks!

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.