1

私は(学習目的で)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? 
4

1 に答える 1

4

プロトタイプではなく、それ自体 を定義postする必要があります。_

_.post = function(url) { ... }
于 2012-09-08T21:27:04.183 に答える