简体   繁体   English

从父 Scope 访问变量的外部函数

[英]External Functions Accessing Variables from Parent Scope

I have a block of code that looks something like this:我有一个看起来像这样的代码块:

const $ = prototype;

(function($){

    myFunc();

})(jQuery)

const myFunc = function () {

    console.log($);

}

In this situation the output is going to be prototype .在这种情况下,output 将成为prototype But I want any child functions to have $ as jQuery.但我希望任何子函数都有$作为 jQuery。 Bear in mind that the functions have many children.请记住,这些函数有很多子函数。

I could pass $ as a parameter, or I could define a new local const in each child function.我可以将$作为参数传递,或者我可以在每个子 function 中定义一个新的本地常量。 Is there any way that I can avoid this and make $ = jQuery available to all child functions?有什么方法可以避免这种情况并使$ = jQuery可用于所有子函数?

maybe you are looking for something like this也许你正在寻找这样的东西

    var previous = null;
var current = null;

const myFunc = function () {

  console.log($);
  console.log(this.v)
  childfunc()

}
const childfunc = function(){
  var $=this.v
  console.log($)     
  childofchild()
}
const childofchild = function(){
  var $=this.v
  console.log($)
}

const $ = 'prototype';

(function($){
  v=$
    myFunc();

})('jQuery');

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

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