rei*_*kus 2 javascript syntax object
我有这个代码,它正在工作.
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();
Run Code Online (Sandbox Code Playgroud)
现在我想要的是将点符号放入对象中,我该怎么做?我试过这个,但是当我调用URL.getCurrent()时它显示为undefined.
function URL(){
this.pattern = /https?:\/\/([^\/]*)\//;
this.current = window.location.href;
this.getCurrent = function(){
return this.current.match(this.pattern)[1];
};
}
Run Code Online (Sandbox Code Playgroud)
我希望有人可以帮助我.
你能做的最简单的事情就是把它放在一个对象字面上.
var URL = {
pattern: /https?:\/\/([^\/]*)\//,
current: window.location.href,
getCurrent: function () {
return this.current.match(this.pattern)[1];
}
}
alert(URL.getCurrent());?
Run Code Online (Sandbox Code Playgroud)