内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list($forumlist, $model = 0, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $model . '-' . $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 不分模型 * @param int $display 0全部CMS栏目 1在首页和频道显示内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list_show($forumlist, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 * @return mixed BBS栏目数据(仅列表) 尚未开放bbs频道功能 */ function forum_list($forumlist) { if (empty($forumlist)) return array(); static $cache = array(); if (isset($cache['bbs_forum_list'])) return $cache['bbs_forum_list']; $cache['bbs_forum_list'] = array(); foreach ($forumlist as $_fid => $_forum) { if ($_forum['type']) continue; $cache['bbs_forum_list'][$_fid] = $_forum; } return $cache['bbs_forum_list']; } // 导航显示的版块 function nav_list($forumlist) { if (empty($forumlist)) return NULL; static $cache = array(); if (isset($cache['nav_list'])) return $cache['nav_list']; foreach ($forumlist as $fid => $forum) { if (0 == $forum['nav_display']) { unset($forumlist[$fid]); } } return $cache['nav_list'] = $forumlist; } ?>Is it possible to change variables inside a closure in JavaScript? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is it possible to change variables inside a closure in JavaScript? - Stack Overflow

programmeradmin0浏览0评论

Let's suppose I have the following:

(function() {
  let myVar = 300;
  window.myFunc = function() {
    console.log(myVar);
  };
})();

Without changing the above code, can you think of a way to change the myVar variable? I mean from the outside, can you do something like:

window.myFunc.__closure__.myVar = 10;

Is this possible?

Let's suppose I have the following:

(function() {
  let myVar = 300;
  window.myFunc = function() {
    console.log(myVar);
  };
})();

Without changing the above code, can you think of a way to change the myVar variable? I mean from the outside, can you do something like:

window.myFunc.__closure__.myVar = 10;

Is this possible?

Share Improve this question edited Apr 19, 2019 at 0:27 Miroslav Glamuzina 4,5872 gold badges21 silver badges33 bronze badges asked Apr 18, 2019 at 23:52 Some GuySome Guy 13.6k22 gold badges62 silver badges93 bronze badges 2
  • 1 Is there a reason you want to do this? Or are you just curious if it's possible. Without modifying the code the answer is no. Or at least, you shouldn't be able to. – Khauri Commented Apr 19, 2019 at 0:11
  • via Dev-tools; just set a break point in any function that can see this variable. via code, No, not possible, not without changing the code; unless they use evil() somewhere where this variable is visible, and you can inject some code. – Thomas Commented Apr 19, 2019 at 0:40
Add a ment  | 

3 Answers 3

Reset to default 4

No, it is not possible because the scope of the variable is the function's block.

The only way of modifying something inside of the closure is thru properties in the current lexical context (in this case the window object).

(function() {
  this.myVar = this.myVar || 300;
  window.myFunc = function() {
    console.log(myVar);
  };
})();

myVar = "Ele";
myFunc();

You could define a global function that changes the variable:

(function() {
  let myVar = 300;
  window.myFunc = function() {
    console.log(myVar);
  };
  window.setMyVar = value => {
    myVar = value;
  };
})();

myFunc();
setMyVar(10);
myFunc();

Or you could return setMyVar:

const setMyVar = (function() {
  let myVar = 300;
  window.myFunc = function() {
    console.log(myVar);
  };
  return value => {
    myVar = value;
  };
})();

myFunc();
setMyVar(100);
myFunc();

The idea of using a closure is to make variable accessible only to a specific scope. If you then want to change it outside of this scope, why not use a class?

eg.

class myClass {
  constructor(var) { this._var = var }
  set var(arg) { this._var = arg }

  yourFunc() { console.log(this._var) }
}

发布评论

评论列表(0)

  1. 暂无评论