How do you define an OOP class in JavaScript?

前端 未结 7 709
野性不改
野性不改 2020-12-20 19:32

Based on my observation, the book that I am reading about JavaScript states that there\'s an OOP with JavaScript? It doesn\'t tell much about it, I mean it wasn\'t explained

7条回答
  •  我在风中等你
    2020-12-20 20:25

    Here are couple different ways

    if (typeof FFX == "undefined") {
        FFX = {};
    }
    
    //Static class
    FFX.Util = ({
         return {
          method:function(){
          }
    })();
    
    FFX.Util.method(); 
    
    
    
    //Instance class
    FFX.Util2 = ({
        // private method
        var methodA=function(){
          alert("Hello");
        };
         return {
          method:function(){
          //Call private method
            methodA();
          }
    });
    var x= new FFX.Util();
    x.method(); 
    

    Another way

    function MyClass(){
    }
    
    /* privileged functions */
    MyClass.prototype.hello = function(){
        alert("Hello");
    }   
    

    Also you could see how jquery, prototype and alike handle classes and see if thats fits you needs.

提交回复
热议问题