下面的代码片段是30秒的代码网站.这是一个初学者的例子,令人尴尬地让我难过.
为什么这样:
const currentURL = () => window.location.href;
什么时候可以这样做?
const currentURL = window.location.href;
解决方法:
第一个将currentURL设置为一个求值为window.location.href的函数,另一个只将currentURL设置为window.location.href.
考虑以下区别:
/*
* A function that will return the current href
* returns {String}
*/
const currentURL1 = () => window.location.href;
/*
* The current href
* @type {String}
*/
const currentURL2 = window.location.href;
console.log(currentURL1); // () => window.location.href
console.log(currentURL2); // https://stacksnippets.net/js
console.log(typeof currentURL1); // function
console.log(typeof currentURL2); // string
currentURL1(); // string
//currentURL2(); // error not a function