Upgrade to Pro — share decks privately, control downloads, hide ads and more …

introduce to Mayjs

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on. →
Avatar for zhongxingdou zhongxingdou
September 25, 2014

introduce to Mayjs

May JavaScript better

Avatar for zhongxingdou

zhongxingdou

September 25, 2014

Other Decks in Programming

Transcript

  1. Ruby的module class  User  <  ActiveRecord::Base      acts_as_votable   end

      ! HaiMeimei  =  User.new   LiLei  =  User.new   HaiMeimei.vote_by  :voter  =>  Lilei,  :vote  =>  'like'
  2. 声明May.js的module var  Voteable  =  $module({      onIncluded:  function(person){  

           person.votes  =  [];      },      voteBy:  function(person,  voter,  vote){          person.votes.push({voter:  voter,  vote:  vote});      }   });
  3. class include module var  User  =  $class({});   User.include(Voteable,  {methodize:

     true});   ! HaiMeimei  =  new  User();   LiLei  =  new  User();   HaiMeimei.voteBy(LiLei,  "like");   !
  4. IModuleOption IModuleOption  =  {     context:  Object,  //回调onIncluded和wrap⽅方法时⽤用  

      methodize:  Boolean,  //是否wrap⽅方法     methodizeTo:  Function,  //wrap⽅方法时指定参数     supports:  Array,  //可以⽀支持到的class和prototype     stopCallback:  Boolean    //停⽌止回调onIncluded()   }   !
  5. $

  6. $(  "button.continue"  ).html(  "Next   Step..."  )   ! $(

     "#button-­‐container  button"  ).on(  "click",              function(  event  )  {                    hiddenBox.show();  });
  7. _

  8. M

  9. var  stringEx  =  {          reverse:  function(s){

                     return  s.split("").reverse().join("");          }   }   ! M.$.reg(stringEx,  String,  {methodize:  true});   ! M.$("abcd").reverse();
  10. ! var  s  =  new  String('abcd');   M.$(s).reverse();   s.reverse;

     //undefined   ⾮非侵⼊入式的扩展对象$!
  11. M(function($,  $$){          "use  strict";   !

           var  stringEx  =  {                  reverse:  function(s){                          return  s.split("").reverse().join("");                  },                  __option__:  {methodize:  true}          }          $.reg(stringEx,  String);   !        var  s  =  $$("abcd");          s.reverse();  //dcba   }); M式写法—strict版!
  12. M(function(){          eval(M());   !    

       var  stringEx  =  {                  reverse:  function(s){                          return  s.split("").reverse().join("");                  },                  __option__:  {methodize:  true}          }          $.reg(stringEx,  String);   !        var  s  =  $$("abcd");          s.reverse();  //dcba   }); M式写法!
  13. methodize function  methodize(fn,  firstParam,  getFirstParam)  {        

     return  function()  {                  //获取第⼀一个参数                  var  p1  =  firstParam  ||  this;                  if(getFirstParam)  p1  =  getFirstParam(p1);   !                //把第⼀一个参数和其他参数放在⼀一起                  var  slice  =  Array.prototype.slice;                  var  args  =  [p1].concat(slice.call(arguments));   !                return  fn.apply(this,  args);          }   }
  14. methodize var  panel  =  {};   var  ContainerEx  =  {

             collapse:  function(ui){              //ui.body.display  =  "none";          }   }   ! panel.collapse  =  methodize(ContainerEx.collapse,                                                        panel);   ! panel.collapse();  
  15. M.$()   M.$$()   M.$.reg()   ! ! $()  

    $$()   $.reg() 局部 全局 M.Wrapper $() $$() $.reg() @__map__ findWrappersByType() M.Wrapper $() $$() $.reg() @__map__ findWrappersByType()
  16. //BaseExtend.js   var  StringEx  =  {        

     truncate:  function(s,  count){              return  s.length  <=  count  ?  s  :  s.substr(0,  count)  +  "…";          },          __option__:  {methodize:  true}   }   M.$.reg(StringEx,  String);   ! M.$("abcdefghijklmn").truncate(6);  //abcdef…   ! !
  17. //BaseExtend.js   var  StringEx  =  {        

     truncate:  function(s,  count){},          __option__:  {methodize:  true}   }   M.$.reg(StringEx,  String);   ! ! ! ! //page.js   M(function($,  $$){   var  nickNameEx  =  {          atName:  function(s){  return  "@"  +  s  }   }   $.reg(nickNameEx,  String,  {methodize:  true});   !        $("abcdefghijklmn").truncate(6);  //abcdef…          $("Jim").atName();  //@Jim   });   ! M.$("Jim").atName;  //undefined  
  18. ! ! //page.js   M(function($,  $$){   var  nickNameEx  =

     {          atName:  function(s){  return  "@"  +  s  }   }   $.reg(nickNameEx,  String,  {methodize:  true});   ! !        M(function($2,  $$2){                  $2.reg({twice:  function(){}},  String,  {methodize:   true});                  $2("abcd").twice();  //abcdabcd   !                $("Jim").atName();  //@Jim                  $2("Jim").atName;  //undefined          });   ! });  
  19. 接⼝口对象 interface IA{ int x; int y; } interface IB{

    int x; int y; } != namespace Ali.biz namespace Jd.biz ⽤用你的代码可以,还要我的代码⾥里打上你公司的标记
  20. 声明May.js的interface var  IPerson  =  $interface({      name:  String,  

           age:  Number,          sayHi:  [IPerson,  String],      interest:  Array   });  
  21. 实现May.js的interface var  Person  =  $class({      initialize:  function(name,  age,

     interest){                  $check($is(IPerson.name,  name));                  $check($is(IPerson.age,  age));                  $check($is(IPerson.interest,  interest));                  …          }          sayHi:  function(person,  saySomething){                    $checkArgu();          //$checkArgu(IPerson,  String);          …      }   });   ! $implement(IPerson,  Person);  
  22. $support() — 检查interface var  IPatient  =  $interface({      name:

     String,          age:  Number   });   ! var  XiaoMing  =  new  Person("xiaoming",  13,  ["swimming","singing"]);   ! var  Hospital  =  {          records:  {},          registration:  function(patient,  department){       $support(IPatient,  patient);                this.records.add(patient,  department);          }   }   !
  23. $overload()        var  jjQuery  =  $overload([String],  function(selector){  

                   return  $(document.querySelectorAll(selector));          }).overload([Element.prototype],function(el){                  return  $(el);          }).overload([HTMLCollection.prototype],function(els){                  return  $(els);          }).overload([String,Element.prototype],  function(selector,   parentElement){                  return  $(parentElement.querySelectorAll(selector));          }).overload([/^#[^  ]*/],function(id){                  return  $(document.getElementById(id));          });
  24. 对象维护: $overwrite,  $overload,  $merge,  $mix   ! 语法糖: $fn,  $run,

     $dsl,  $ensure   ! 对象创建: $clone,  $enum,  $obj,  $func   ! ⾯面对对象: $class   $interface,  $support,  $implement     $module,  $include   ! 对象wrap:   $methodize,  $wrapper,     M.$,  M.$$,  $,  $$   ! 类型检查: $is,  $check,  $checkArgu,  $hasProto
  25. May.js • 3k,源码不超过1500⾏行 • 三年,⼀一个⼈人,450多个commit • 可通过npm或者bower下载 • npm install

    mayjs • bower intall mayjs • ⽂文档和测试全 • http://zhongxingdou.github.io/mayjs/