函数(点)值[Javascript]



我如何创建一个函数与值之后?

function gotolocation(href) {
href = href + "?search=stackoverflow";
window.location = href;
}
gotolocation.href = "https://google.com";

你可以使用Setter

const gotolocation = {
set href(url) {
url += "?search=stackoverflow";
window.location = url;
}
};
gotolocation.href = "https://google.com";

,但在这种情况下,gotolocation将是一个具有绑定到href属性的Setter函数的对象,而不是函数本身。


如果你的问题是:"如何使用这个函数">
那么答案很简单:你的函数接受一个参数,所以传递一个:

gotolocation("https://google.com");

你可以用function创建一个对象,就像OOP中的class;

const location = {
href: "https://facebook.com",
goto: () => {
let link = location.href + "?search=stackoverflow";
window.location = link;
}
}
location.href = "https://google.com";
location.goto();

您可以将值传递给函数

gotolocation("https://google.com");

最新更新