reitermarkus reitermarkus -3 years ago 116
Javascript Question

How to combine this to one javascript Object?

I have this Code, and it's working.

var URL = new Object();

URL.pattern = /https?:\/\/([^\/]*)\//;
URL.current = window.location.href;

URL.getCurrent = function(){
return this.current.match(this.pattern)[1];
};

var thisDomain = URL.getCurrent();


Now what I want is to put the dot notations into the object, how do I do that? I tried this, but it's showing undefined when I call URL.getCurrent().

function URL(){

this.pattern = /https?:\/\/([^\/]*)\//;
this.current = window.location.href;

this.getCurrent = function(){
return this.current.match(this.pattern)[1];
};
}


I hope someone can help me out.

Answer Source

The easiest thing you could do, is putting it n an object literal.

http://jsfiddle.net/wJQb6/

var URL = {
    pattern: /https?:\/\/([^\/]*)\//,
    current: window.location.href,
    getCurrent: function () {
        return this.current.match(this.pattern)[1];
    }
}

alert(URL.getCurrent());‚Äč
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download