V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
SolidZORO
V2EX  ›  问与答

請教 js 模塊化問題。各模塊 js 被 gulp 壓成 app.js 後, HTML 頁面如何判斷並讀取 app.js 的其中一塊代碼?

  •  
  •   SolidZORO · 2016-05-14 14:19:38 +08:00 · 1398 次点击
    这是一个创建于 2875 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如有 article, product 兩個模塊

    目錄結構:

    - article
    |---- index.html
    |---- article.js
    |---- article.css
    - product
    |---- index.html
    |---- product.js
    |---- product.css
    

    載入任意模塊 index.html 都只讀取 app.js ,比如讀取 article/index.html 的時候,只想 article/article.js 裡的代碼,而 product/product.js 不要執行。

    現在我的做法是:

    1 / 在 article/index.html<body> 上加入 class ,變成 <body class="body-article">

    2 / article/article.js 裡 寫上

    if (!$_body.hasClass("body-article")) {
    	return false;
    }
    

    但感覺這樣的方法不規範,這導致 IDE (eg: PhpStorm) 不能很好的分析 js 之間的關係。請問怎樣的寫法才規範, IDE 能正常分析 js 呢?

    1 条回复    2016-05-14 14:48:35 +08:00
    hxtheone
        1
    hxtheone  
       2016-05-14 14:48:35 +08:00
    这个感觉也只能从代码层面做隔离, 话说为什么有这种需求呢? 全局变量被污染了?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5161 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 05:42 · PVG 13:42 · LAX 22:42 · JFK 01:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.