简体   繁体   English

JQUERY和JAVASCRIPT OOP

[英]JQUERY and JAVASCRIPT OOP

I have a jquery function: 我有一个jQuery函数:

$(document).ready(function(){
$("#expand_label_object").on("click", function(){ 
$(this).toggleClass("col-3");
$(this).toggleClass("col-12"); 
$("#expand_label_info").toggleClass("hidden");
});
});

It simply uses toggleClass to give me the desired output. 它只是使用toggleClass给我所需的输出。

I have a lot of elements that I want to use this function on, the problem is using the procedural way of programming I will have to write this function for each element I would like to perform the function on. 我有很多要使用此功能的元素,问题是使用程序化的编程方式,我必须为要在其上执行功能的每个元素编写此功能。 For example: 例如:

$(document).ready(function(){
$("#expand_Date_object").on("click", function(){
$(this).toggleClass("col-3");
$(this).toggleClass("col-12");
$("#expand_Date_info").toggleClass("hidden");
});
});

As you can see the only thing that changed was the #expand_label_object and #expand_label_info. 如您所见,唯一改变的是#expand_label_object和#expand_label_info。

Does anyone have any advise for how I can go about creating an object in javascript that will be able to perform the work of this function and then I can simply instantiate a new object of this "class" that can perform the task on the new information provided in example #expand_Date_Object. 有没有人对我如何在javascript中创建一个能够执行此功能的对象的方法有任何建议,然后我可以简单地实例化此“类”的新对象,该对象可以在新信息上执行任务在示例#expand_Date_Object中提供。

Or should I write a plugin for jquery that will handle this. 或者我应该为jquery写一个插件来处理这个问题。 If so how? 如果可以,怎么办?

  1. Can I create an object and prototype that object? 我可以创建一个对象并对该对象进行原型设计吗?
  2. Should I use a constructor? 我应该使用构造函数吗?
  3. How will I be able to handle the jquery library in a prototype framework? 如何在原型框架中处理jquery库?

You can create a named function, call function setting this to current clicked element utilizing Function.prototype.call() , pass selector string as parameter to function 您可以创建一个名为函数,调用函数设置this来利用当前点击的元素Function.prototype.call()通过选择字符串作为参数的功能

$(document).ready(function(){

  function handleClick(selector) { 
    $(this).toggleClass("col-3");
    $(this).toggleClass("col-12"); 
    $(selector).toggleClass("hidden");
  }   

  $("#expand_label_object").on("click", function() { 
    handleClick.call(this, "#expand_label_info")
  });

  $("#expand_Date_object").on("click", function() { 
    handleClick.call(this, "#expand_Date_info")
  });

});

It seems that you want an elegant code based on OOP paradigm ; 似乎您想要一个基于OOP范例的优雅代码; if so , you could take advantage from ES6 syntax: 如果是这样,您可以利用ES6语法:

1. Build Abstract API : 1.构建抽象API:

   //- -- JQuery Class

   class $Class{
         constructor(selector,parent){
             this.selector=selector;
             this.parent= parent;
         }
         get element(){
             return (this.parent)?$(this.selector,this.parent):$(this.selector);
          }
         on(event,callback){

              this.element.on(event,callback);
         }

   }

    //Common service (contains static methods)

        class CommonService{

            static toggle1(htmlElement){
                 $(htmlElement).toggleClass("col-3");
                 $(htmlElement).toggleClass("col-12");
                 $("#expand_Date_info").toggleClass("hidden");

             }
         }

2. Use API : 2.使用API​​:

 var exp_label=new $Class('#expand_label_object');
 exp_label.on('click',function(){ CommonService.toggle1(this)} ) 

  // the same of the other code , it will be : 
 var exp_date=new $Class('#expand_Date_object');
 exp_date.on('click',function(){ CommonService.toggle1(this)} ) 

Hope this OOP that you target. 希望您以此为目标。

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

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