gpt4 book ai didi

javascript - 如何将其合并为一个 javascript 对象?

转载 作者:行者123 更新时间:2023-12-04 05:52:04 26 4
gpt4 key购买 nike

我有此代码,并且可以正常使用。

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];
};
}

我希望有人能帮助我。

最佳答案

你能做的最简单的事情就是把它放在一个对象字面量中。

http://jsfiddle.net/wJQb6/

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com