我有此代码,并且可以正常使用。
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();
现在我想要的是将点符号放入对象中,我该怎么做?我试过了,但是当我调用 URL.getCurrent() 时它显示未定义。
function URL(){
this.pattern = /https?:\/\/([^\/]*)\//;
this.current = window.location.href;
this.getCurrent = function(){
return this.current.match(this.pattern)[1];
};
}
我希望有人能帮助我。
最佳答案
你能做的最简单的事情就是把它放在一个对象字面量中。
var URL = {
pattern: /https?:\/\/([^\/]*)\//,
current: window.location.href,
getCurrent: function () {
return this.current.match(this.pattern)[1];
}
}
alert(URL.getCurrent());
关于javascript - 如何将其合并为一个 javascript 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11753801/