私は(学習目的で)JQueryライブラリを模倣しようとしていますが、次のことにこだわっています:
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);
}
}
これを次のように使用できます。
_("a1").append(' deescription');
しかし、コンストラクターを呼び出さずにポスト関数を使用できるようにしたい:
_.post('url') //post is in the prototype but is undefined because the constructor is not called right?