简体   繁体   English

javascript / jquery oop学习

[英]javascript/jquery oop learning

I am very new to OOP. 我是OOP的新手。 my understanding is that, it is like CSS class, we have classes then we could apply/use it. 我的理解是,就像CSS类,我们有一些类可以应用/使用它。

this is my code. 这是我的代码。

$(".mybutton").click(function(){
    $(this).siblings('.mybutton').removeClass('active').end().addClass('active');           
});


$('.mybutton, .second').click(function(event){
    $('#thisDiv').hide().filter(this.hash).show();
    event.preventDefault();
});

$('.second').on("click", function(){
$(".mybutton").eq(1).siblings('.mybutton').removeClass('active').end().addClass('active');
    $("html, body").animate({
        scrollTop: $('#contact').offset().left - 20},
        800);
});

how should I convert them to oop style? 我应该如何将它们转换为oop风格?

var Doit = {
    init:function(){
        $(".mybutton").on("click", this.active);
    },
    active:function(){
    $(this)
      .siblings('.mybutton')
          .removeClass('active')
              .end().addClass('active');
    },
    filter:function {


    }


};

Doit.init();

if someone could give me a hint, if my code really needs to be changed to OOP ? 如果有人可以给我提示,是否真的需要将我的代码更改为OOP?

Thanks 谢谢

Try it like, 试试吧,

var Doit = function(){
    this.init=function(){
        $(".mybutton").on("click", this.active);
    },
    this.active=function(){
        $(this)
          .siblings('.mybutton')
              .removeClass('active')
                  .end().addClass('active');
    },
    this.filter=function() {
        //code
    }
};

Read More Javascript Object Oriented 阅读更多Javascript Oriented

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

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