0

my problem is that I want to call a function like:

$('div').doSomething('xyz');

and my js-code is:

var $ = function(element) {
    var doSomething = function(xyz, xzy, zxy) {
        alert(xyz + element);
    };
};

but that does not work (I'm new to js anonymous functions), where's the error?

Thanks for help!

1

1 Answer 1

1

Try

var $ = function(element) {
    // if the function is called without being called as a constructor,
    // then call as a constructor for us.
    // (partially borrowed from http://stackoverflow.com/questions/4556110/creating-a-jquery-like-object )
    if (this.constructor !== $) {
        return new $(element);
    }
    this.doSomething = function(txt) {
        alert(txt + element);
    };
};
Sign up to request clarification or add additional context in comments.

11 Comments

firebug output: $('div') is undefined
@idealmachine Edited to fix using your comment. Oops.
Will not work in IE - it doesn't support the non-standard proto
Thanks for your immediate help!
I tried removing __proto__ to access this.constructor directly and it seems to be working in both Chrome and IE now.
|

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.