2

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.

2
  • 1
    Also, I hope you at least tick an answer this time Commented Aug 1, 2012 at 6:41
  • I'm sorry guys, I didn't know that. I'll accept them from now on. =) Commented Aug 1, 2012 at 6:57

5 Answers 5

5

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());​ 
Sign up to request clarification or add additional context in comments.

3 Comments

literal annotation for the win. I'm not sure what OP wants and this seems more like a rewrite of the original code, anyway this is much more effective and what OP is trying is most likely impossible (you can't access function objects' nested functions as you do for regular object).
Hmm, already tried literal annotation, didn't work either. But now it's working!
And yes, it is a rewrite of the original.
2
function URL(){ this.pattern = /https?:\/\/([^\/]*)\//; this.current = window.location.href; this.getCurrent = function(){ return this.current.match(this.pattern)[1]; }; } 

With this, you have an empty constructor. The function URL itself has no properties, you will need to create an instance:

var url = new URL; url.getCurrent(); 

Yet, I'd recommend the following constructor, which includes inheritance:

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

If you want a static object, just use an object literal:

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

1 Comment

Thanks, the static object is what I wanted.
1

You still need to instantiate.

var url = new URL; 

1 Comment

Ok, now I know that. I guess everyone was a beginner once. =)
1

There is not static methods in javascript, but you can use a singleton.

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

That will allow you to have access to the URL proptotype and contructor if ever need them.

Comments

0

If you need to use classic OOP You can do something like this:

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

3 Comments

pattern could be on the prototype as well (see my answer)
...but the empty constructor looks odd.
We may consider it as a class declaration. If it still looks odd initialize the fields there :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.