-1

Possible Duplicate:
Constructors in Javascript objects

im trying to learn how to create class's in javascript. I found that is very diffuclt for me to understand it.

now, i want to know if is possible to create a constractor in javascript, like we can do in c# or other programming languages.

i tried few things:

way 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;
}

way 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;
    }
}

both of class's should take a URL, and just get the name from him with out the www. or the .com

i want to know if i can design a class, that i can create an instance like so:

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

(sorry for my english)

Community
  • 1
  • 1
samy
  • 1,949
  • 6
  • 39
  • 64

2 Answers2

2

You're real close. The problem with your first form is simply that you are not setting the url property with the _url parameter.

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

Here's the fiddle for the first form: http://jsfiddle.net/BCnfx/

The problem with the second form is two-fold. The main function should be called "SiteProfile" if you still want to instantiate it as such. The second problem is that you need to initialize the url property by passing in the url to the Site method.

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

Here's the fiddle for the second form: http://jsfiddle.net/BCnfx/1/

pete
  • 24,141
  • 4
  • 37
  • 51
  • thank pete, i understand now that there is a constractors in javascript afther all. – samy Oct 08 '12 at 13:57
1

in your first example:

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

then you will be able to do :

var site = new SiteProfile("www.google.co.il");
 document.write(site.name);
urbananimal
  • 495
  • 3
  • 9