Javascript 私人活动协助
Javascript Private Function Assistance
我在掌握如何将 javascript functions/variables 设为私有时遇到问题。我需要如何编辑它才能使 functions/variables 像这样私有化。
<html>
<head>
<style>
#panel, .flip {
font-size: 16px;
padding: 10px;
text-align: center;
background-color: #4CAF50;
color: white;
border: solid 1px #a6d8a8;
margin: auto;
}
#panel {
display: none;
}
</style>
</head>
<body>
<p class="flip" onclick="myFunction()">Click to show panel</p>
<div id="panel">
<p>This panel contains a div element, which is hidden by default (display: none).</p>
<p>It is styled with CSS and we use JavaScript to show it (display: block).</p>
<p>How it works: Notice that the p element with class="flip" has an onclick attribute attached to it. When the user clicks on the p element, a function called myFunction() is executed, which changes the style of the div with id="panel" from display:none (hidden) to display:block (visible).</p>
<p>You will learn more about JavaScript in our JavaScript Tutorial.</p>
</div>
<script>
function myFunction() {
document.getElementById("panel").style.display = "block";
}
</script>
</body>
</html>
你不能。
JavaScript 没有任何访问修饰符的概念(除了模块中的 export
,但具有内联脚本的 <script>
元素不是 JavaScript 模块)。
此外,OOP 语言中的 private
修饰符仅对 class
/struct
类型有意义,对 free-functions 无效(因为所有自由函数都是全局范围的),所以private function myFunction() { ... }
的想法是没有意义的。
目前在 JavaScript 生态系统中,当使用 JavaScript class
(这只是 prototype
声明的语法糖)时,通常表示 "private" 属性(包括函数)使用前导下划线 - 但这是约定而非语言功能,也不会阻止从另一个脚本调用函数:
class Foobar {
doSomething() { // <-- "public"
// ...
}
_doSomethingElse() { // <-- the leading underscore is a hint to consumers not to use this property directly, but they can still call it if they want to.
// ...
}
}
var f = new Foobar();
f.doSomething();
f._doSomethingElse(); // <-- nothing stops a consumer from calling this function-property.
解决方法:
请注意,您可以 有一个 JavaScript 对象(使用 class
或只是一个 POJO) 无法访问 匿名函数,前提是您可以以不公开它们的方式连接它们 - 但这种方法的缺点是您自己的代码也不能直接调用它们。这种方法可用于设置事件处理程序而不会污染全局命名空间:
class Foobar {
constructor() {
(function() { // This is an IIFE
const button = document.getElementById( 'foobar' );
button.addEventListener( 'click', function() {
alert("clicked!);
} );
})();
}
}
在上面的代码中,IIFE 和 click
事件处理函数现在无法直接调用,因为它们没有名称,并且在脚本运行后它们将不在范围内。
您可以通过将函数包装在特定范围内来创建函数 "private"。
在您的示例中,这意味着将其包装在 immediately invoked function expression:
(function () {
function myFunction() {
document.getElementById("panel").style.display = "block";
}
// myFunction is only available within the outer function context.
})();
这也意味着以下将不再有效:
<p class="flip" onclick="myFunction()">Click to show panel</p>
myFunction
不再公开可用,因此现在单击时会抛出错误。但是,当您仍处于与 myFunction
:
相同的范围内时,您可以通过 addEventListener
设置它
(function () {
function myFunction() {
document.getElementById("panel").style.display = "block";
}
for(const p of document.querySelectorAll("p.flip")) {
p.addEventListener("click", myFunction);
}
})();
我在掌握如何将 javascript functions/variables 设为私有时遇到问题。我需要如何编辑它才能使 functions/variables 像这样私有化。
<html>
<head>
<style>
#panel, .flip {
font-size: 16px;
padding: 10px;
text-align: center;
background-color: #4CAF50;
color: white;
border: solid 1px #a6d8a8;
margin: auto;
}
#panel {
display: none;
}
</style>
</head>
<body>
<p class="flip" onclick="myFunction()">Click to show panel</p>
<div id="panel">
<p>This panel contains a div element, which is hidden by default (display: none).</p>
<p>It is styled with CSS and we use JavaScript to show it (display: block).</p>
<p>How it works: Notice that the p element with class="flip" has an onclick attribute attached to it. When the user clicks on the p element, a function called myFunction() is executed, which changes the style of the div with id="panel" from display:none (hidden) to display:block (visible).</p>
<p>You will learn more about JavaScript in our JavaScript Tutorial.</p>
</div>
<script>
function myFunction() {
document.getElementById("panel").style.display = "block";
}
</script>
</body>
</html>
你不能。
JavaScript 没有任何访问修饰符的概念(除了模块中的 export
,但具有内联脚本的 <script>
元素不是 JavaScript 模块)。
此外,OOP 语言中的 private
修饰符仅对 class
/struct
类型有意义,对 free-functions 无效(因为所有自由函数都是全局范围的),所以private function myFunction() { ... }
的想法是没有意义的。
目前在 JavaScript 生态系统中,当使用 JavaScript class
(这只是 prototype
声明的语法糖)时,通常表示 "private" 属性(包括函数)使用前导下划线 - 但这是约定而非语言功能,也不会阻止从另一个脚本调用函数:
class Foobar {
doSomething() { // <-- "public"
// ...
}
_doSomethingElse() { // <-- the leading underscore is a hint to consumers not to use this property directly, but they can still call it if they want to.
// ...
}
}
var f = new Foobar();
f.doSomething();
f._doSomethingElse(); // <-- nothing stops a consumer from calling this function-property.
解决方法:
请注意,您可以 有一个 JavaScript 对象(使用 class
或只是一个 POJO) 无法访问 匿名函数,前提是您可以以不公开它们的方式连接它们 - 但这种方法的缺点是您自己的代码也不能直接调用它们。这种方法可用于设置事件处理程序而不会污染全局命名空间:
class Foobar {
constructor() {
(function() { // This is an IIFE
const button = document.getElementById( 'foobar' );
button.addEventListener( 'click', function() {
alert("clicked!);
} );
})();
}
}
在上面的代码中,IIFE 和 click
事件处理函数现在无法直接调用,因为它们没有名称,并且在脚本运行后它们将不在范围内。
您可以通过将函数包装在特定范围内来创建函数 "private"。
在您的示例中,这意味着将其包装在 immediately invoked function expression:
(function () {
function myFunction() {
document.getElementById("panel").style.display = "block";
}
// myFunction is only available within the outer function context.
})();
这也意味着以下将不再有效:
<p class="flip" onclick="myFunction()">Click to show panel</p>
myFunction
不再公开可用,因此现在单击时会抛出错误。但是,当您仍处于与 myFunction
:
addEventListener
设置它
(function () {
function myFunction() {
document.getElementById("panel").style.display = "block";
}
for(const p of document.querySelectorAll("p.flip")) {
p.addEventListener("click", myFunction);
}
})();