search

Home  >  Q&A  >  body text

javascript - seajs包装jquery插件遇到的问题

第一段代码

define(function(require){
    var $ = require('jquery');
    console.log($) // null
    console.log(this.$) // 打印jquery没问题
    var t = 2;
    console.log(t) //2 ,没问题
})

第一个打印的为什么会是null?这个不理解。


第二段代码

define(function(require){
    var $ = require('jquery');
    require("./jquery.plugin")(this.$)
    console.log(this.$.fn.plugin_method) // undefined
    console.log(this.$.fn)
})

打印this.$.fn,在控制台可以看到plugin_method 方法,可是为什么直接打印this.$.fn.plugin_method,这个方法又没了?

PHPzPHPz2902 days ago337

reply all(3)I'll reply

  • PHP中文网

    PHP中文网2017-04-10 14:48:43

    我觉得楼上的说法亮了。
    AMD 是 RequireJS 在推广过程中对模块定义的规范化产出。
    CMD 是 SeaJS 在推广过程中对模块定义的规范化产出。
    jQuery 是支持AMD的但不支持CMD,所以要包装,具体看https://github.com/seajs/seajs/issues/971。

    reply
    0
  • 迷茫

    迷茫2017-04-10 14:48:43

    你的jquery文件也得包装一下,jquery支持cmd和amd的方式不是很适合seajs

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-10 14:48:43

    jquery源码中有一段:

    "function"==typeof define&&define.amd&&define("jquery",[],function(){return n})

    在其后添加:

    "function"==typeof define&&define.cmd&&define("jquery",[],function(){return n})

    即可。

    reply
    0
  • Cancelreply