Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 243
reitermarkus Avatar asked Feb 27 '26 12:02

reitermarkus


1 Answers

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());​
like image 156
Robin Drexler Avatar answered Mar 01 '26 02:03

Robin Drexler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!