Samson
Samson

Reputation: 2821

Javascript JQuery-like library

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? 

Upvotes: 1

Views: 135

Answers (1)

Matt Greer
Matt Greer

Reputation: 62027

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

_.post = function(url) { ... }

Upvotes: 4

Related Questions