1

I'm trying to mimic a JQuery library (for learning purposes) and I'm stuck with the following:

function _(id){
var about={
version:"1.0",
author:"Samson"
}
if (this===window)
    return new _(id);
if (!id) 
    return about;
if (typeof id=="string")
    this.element=document.getElementById(id);
else
    this.element=id;

return this;
}

_.prototype={
    append:function(str){
        this.element.innerHTML+=str;
        return this;
    },
    post:function(url){
        alert('posting to: '+url);
    }
}

I can use this like this:

_("a1").append(' deescription');

But I want to be able to use the post function without invoking the constructor :

_.post('url') //post is in the prototype but is undefined because the constructor is not called right? 
1
  • @arxanas It is in the sense that it has mechanisms for extension. I'd call it a library, but the plugin and extensibility bits are frameworky. In any case, OP is saying they're building a framework, not that jQuery is one. Commented Sep 8, 2012 at 21:38

1 Answer 1

4

You will need to define post on _ itself, not its prototype.

_.post = function(url) { ... }
Sign up to request clarification or add additional context in comments.

1 Comment

No problem, JS is a very flexible language and you can mold it in many ways (one reason why I like it so much). But it can be a little much to wrap around at times.

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.