Reputation: 2821
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
Reputation: 62027
You will need to define post
on _
itself, not its prototype.
_.post = function(url) { ... }
Upvotes: 4