【问题标题】:javascript - creating netrual consractor [duplicate]javascript - 创建中性构造函数
【发布时间】:2012-09-28 18:34:24
【问题描述】:

可能重复:
Constructors in Javascript objects

我正在尝试学习如何在 javascript 中创建类。我发现这对我来说很难理解。

现在,我想知道是否可以在 javascript 中创建一个构造函数,就像我们可以在 c# 或其他编程语言中那样。

我尝试了几件事:

方式一:

    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

(对不起我的英语)

【问题讨论】:

    标签: javascript


    【解决方案1】:

    在你的第一个例子中:

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

    那么你就可以做到:

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

    【讨论】:

    • 奇怪,因为我尝试运行它,但没有任何反应。 (没看到名字)
    • this.url = _url;
    • 谢谢,现在我知道我做了什么。所以毕竟有 javascript 的承包商。酷!
    • 我第一个选择你是因为你是第一个,但我猜你是对的,他应该得到它。但你得到了支持,非常感谢你! :)
    【解决方案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/

    第二种形式的问题是双重的。如果您仍想这样实例化主函数,则应将其称为“SiteProfile”。第二个问题是你需要通过将url传递给Site方法来初始化url属性。

    //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
    

    这是第二种形式的小提琴:http://jsfiddle.net/BCnfx/1/

    【讨论】:

    • 谢谢皮特,我现在明白了,在 JavaScript 中毕竟有​​一个承包商。
    猜你喜欢
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 2015-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多