Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8662661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:47:14+00:00 2026-06-12T16:47:14+00:00

Possible Duplicate: Constructors in Javascript objects im trying to learn how to create class’s

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:47:15+00:00Added an answer on June 12, 2026 at 4:47 pm

    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/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Can constructors be async I have a class Example that pulls several
Possible Duplicate: Calling virtual method in base class constructor Calling virtual functions inside constructors
Possible Duplicate: Best practices for static constructors I want to create an object similar
Possible Duplicate: Why can’t I create an abstract constructor on an abstract C# class?
Possible Duplicate: How to “properly” create a custom object in JavaScript? Is it possible
Possible Duplicate: Why doesn't the CLR always call value type constructors Found next code
Possible Duplicate: Abstract class constructor in Java When we can't create an instance of
Possible Duplicate: Why can’t I create an abstract constructor on an abstract C# class?
Possible Duplicate: Create an empty object in JavaScript with {} or new Object()? When
Possible Duplicate: How to use base class's constructors and assignment operator in C++? class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.