简体   繁体   English

如何将其与一个javascript对象组合?

[英]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(). 我试过这个,但是当我调用URL.getCurrent()时它显示为undefined。

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. 我希望有人可以帮助我。

The easiest thing you could do, is putting it n an object literal. 你能做的最简单的事情就是把它放在一个对象字面上。

http://jsfiddle.net/wJQb6/ http://jsfiddle.net/wJQb6/

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

alert(URL.getCurrent());​
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: 函数URL本身没有属性,您需要创建一个实例:

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();

你仍然需要实例化。

var url = new URL;

There is not static methods in javascript, but you can use a singleton. javascript中没有静态方法,但您可以使用单例。

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. 这将允许您在需要时访问URL proptotype和contructor。

If you need to use classic OOP You can do something like this: 如果你需要使用经典的OOP你可以这样做:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM