-1

重複の可能性:
Javascript オブジェクトのコンストラクター

javascriptでクラスを作成する方法を学ぼうとしています。私はそれを理解するのが非常に困難であることがわかりました。

ここで、c# や他のプログラミング言語でできるように、javascript でコンストラクターを作成できるかどうかを知りたいです。

私はいくつかのことを試しました:

方法 1:

    function SiteProfile(_url) {
    this.url = "";
    this.name = this.ExtractNameFromURL();
}

    SiteProfile.prototype.ExtractNameFromURL = function () {
        var firstDOT = this.url.indexOf(".");
        var secondDOT = this.url.indexOf(".", firstDOT + 1);
        var theName = "";
        for (var i = firstDOT + 1; i < secondDOT; i++) {
            theName += this.url[i];
        }
        return theName;
}

方法 2:

function Site() {

    this.url = "";
    this.name = "";

    this.Site = function (_url) {
        this.url = _url;
        this.name = this.ExtractNameFromURL();
    }

    this.ExtractNameFromURL = function () {
        var firstDOT = this.url.indexOf(".");
        var secondDOT = this.url.indexOf(".", firstDOT + 1);
        var theName = "";
        for (var i = firstDOT + 1; i < secondDOT; i++) {
            theName += this.url[i];
        }
        return theName;
    }
}

どちらのクラスも URL を受け取り、www なしで彼から名前を取得する必要があります。または.com

クラスを設計できるかどうか、次のようにインスタンスを作成できるかどうかを知りたい:

 var site = new SiteProfile("www.google.co.il");
 document.write(site.name); // becuse, this do nothing

(私の英語でごめんなさい)

4

2 に答える 2

2

あなたは本当に近いです。最初のフォームの問題は、パラメーターでurlプロパティを設定していないことです。_url

function SiteProfile(_url) {
    //change the line below to:
    //this.url = _url;
    this.url = "";
    this.name = this.ExtractNameFromURL();
}

SiteProfile.prototype.ExtractNameFromURL = function() {
    var firstDOT = this.url.indexOf(".");
    var secondDOT = this.url.indexOf(".", firstDOT + 1);
    var theName = "";
    for (var i = firstDOT + 1; i < secondDOT; i++) {
        theName += this.url[i];
    }
    return theName;
}
var site = new SiteProfile("www.google.co.il");
document.write(site.name); // with the change above, this will behave as expected

最初のフォームのフィドルは次のとおりです: http://jsfiddle.net/BCnfx/

2 番目の形式の問題は 2 つあります。メイン関数を「SiteProfile」としてインスタンス化する場合は、この関数を呼び出す必要があります。2 つ目の問題は、URL をメソッドurlに渡してプロパティを初期化する必要があることです。Site

//function below should be called "SiteProfile", not "Site"
function Site() {
    this.url = "";
    this.name = "";

    this.Site = function(_url) {
        this.url = _url;
        this.name = this.ExtractNameFromURL();
    };

    this.ExtractNameFromURL = function() {
        var firstDOT = this.url.indexOf(".");
        var secondDOT = this.url.indexOf(".", firstDOT + 1);
        var theName = "";
        for (var i = firstDOT + 1; i < secondDOT; i++) {
            theName += this.url[i];
        }
        return theName;
    };
}

//now instantiate like this instead.
var site = new SiteProfile();
site.Site("www.google.co.il");
document.write(site.name); // with the changes above, this will behave as expected

2 番目の形式のフィドルは次のとおりです: http://jsfiddle.net/BCnfx/1/

于 2012-10-08T13:51:09.503 に答える
1

あなたの最初の例では:

function SiteProfile(_url) {
    this.url = _url;
    this.name = this.ExtractNameFromURL();
}

その後、次のことができます。

var site = new SiteProfile("www.google.co.il");
 document.write(site.name);
于 2012-10-08T13:48:46.913 に答える