javascript-创建网络用户

javascript-创建网络用户,javascript,Javascript,可能重复: 我试图学习如何用javascript创建类。我发现我很难理解它 现在,我想知道是否可以像在c#或其他编程语言中一样,用javascript创建构造函数 我尝试了几件事: 方式1: function SiteProfile(_url) { this.url = ""; this.name = this.ExtractNameFromURL(); } SiteProfile.prototype.ExtractNameFromURL = function

可能重复:

我试图学习如何用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;
}

(对不起我的英语)

在您的第一个示例中:

function SiteProfile(_url) {
    this.url = _url;
    this.name = this.ExtractNameFromURL();
}
然后您将能够执行以下操作:

var site = new SiteProfile("www.google.co.il");
 document.write(site.name);

你真的很接近。第一个表单的问题只是没有使用
\u 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

这是第二种形式的提琴:

wierd,因为我试着运行它,但什么也没发生。(没有看到名称)this.url=\u url;谢谢,现在我明白我在做什么了。因此,毕竟javascript还有构造函数。凉的我第一个选择你是因为你是第一个,但我想你是对的,他应该得到。但是你站起来投票,并向你表示衷心的感谢!:)谢谢pete,我现在明白javascript中毕竟有一个构造函数。
var site = new SiteProfile("www.google.co.il");
 document.write(site.name);
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
//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