有没有办法在 Javascript 中将传递的参数类型提示给 IDE(在我的例子中是 Webstorm)?
Is there a way to hint the passed argument type to IDE (in my case Webstorm) in Javascript?
我有以下 HTML
文件,其中包含一些 JavaScript
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Index</title>
</head>
<body>
<div id="root"></div>
<div>
<label for="new-content">
<textarea id="new-content" style="width: 400px; height: 100px"></textarea>
</label>
<br/>
<button onclick="addContent(document.getElementById('new-content'))">Submit</button>
</div>
<script>
function addContent(/*HTMLTextAreaElement*/content) {
alert(content.value);
}
</script>
</body>
</html>
我喜欢提示 Webstorm(或 IntelliJ 或 Eclipse)content
的类型在 function addContent
中的方式,但我不喜欢我无法分辨它在其中的方式onclick
,这会导致以下警告:
这是我的第一个世界问题:我可以在参数中提示 document.getElementById('new-content')
的类型吗?
我通过反复试验找到了一个方法,以下似乎有效:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Index</title>
</head>
<body>
<div id="root"></div>
<div>
<label for="new-content">
<textarea id="new-content" style="width: 400px; height: 100px"></textarea>
</label>
<br/>
<button onclick="addContent(getNewContent())">Submit</button>
</div>
<div id="content"></div>
<script>
function getNewContent() {
return document.getElementById('new-content');/*HTMLTextAreaElement*/
}
function addContent(/*HTMLTextAreaElement*/content) {
document.getElementById('content').innerHTML =
document.getElementById('content').innerHTML +
'<p>'.concat(content.value).concat('</p>');
}
</script>
</body>
</html>
我有以下 HTML
文件,其中包含一些 JavaScript
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Index</title>
</head>
<body>
<div id="root"></div>
<div>
<label for="new-content">
<textarea id="new-content" style="width: 400px; height: 100px"></textarea>
</label>
<br/>
<button onclick="addContent(document.getElementById('new-content'))">Submit</button>
</div>
<script>
function addContent(/*HTMLTextAreaElement*/content) {
alert(content.value);
}
</script>
</body>
</html>
我喜欢提示 Webstorm(或 IntelliJ 或 Eclipse)content
的类型在 function addContent
中的方式,但我不喜欢我无法分辨它在其中的方式onclick
,这会导致以下警告:
这是我的第一个世界问题:我可以在参数中提示 document.getElementById('new-content')
的类型吗?
我通过反复试验找到了一个方法,以下似乎有效:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Index</title>
</head>
<body>
<div id="root"></div>
<div>
<label for="new-content">
<textarea id="new-content" style="width: 400px; height: 100px"></textarea>
</label>
<br/>
<button onclick="addContent(getNewContent())">Submit</button>
</div>
<div id="content"></div>
<script>
function getNewContent() {
return document.getElementById('new-content');/*HTMLTextAreaElement*/
}
function addContent(/*HTMLTextAreaElement*/content) {
document.getElementById('content').innerHTML =
document.getElementById('content').innerHTML +
'<p>'.concat(content.value).concat('</p>');
}
</script>
</body>
</html>