简体   繁体   English

覆盖或扩展JavaScript中的闭包功能

[英]Override or Extend the closure function in JavaScript

Just wondering about overriding the closure functions behavior. 只是想知道重写闭包函数的行为。

eg How can i override the baz() function in below snippet? 例如,如何覆盖下面的代码片段中的baz()函数?

(function() { (function(){

  var foo = 'Hello'; var bar = 'World!' function baz(){ return foo + ' ' + bar; } 

})(); })();

If you are outside the closure, you cannot override the baz() function that is inside the closure. 如果您在闭包外部,则无法覆盖闭包内部的baz()函数。 Closures provide privacy and that works for local functions like it works for local variables. 闭包提供了隐私,并且适用于局部函数,就像适用于局部变量一样。

If you are inside the closure, you can define a new version of baz() inside the closure or edit the code inside of baz() . 如果你是瓶盖内,您可以定义新版本的baz()的闭包或编辑里面的代码baz()

Related question: Can I "extend" a closure-defined "class" in Javascript? 相关问题: 我可以在Javascript中“扩展”闭包定义的“类”吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM