代码之家  ›  专栏  ›  技术社区  ›  sasori

如何在javascript中定义oop类?

  •  6
  • sasori  · 技术社区  · 14 年前

    根据我的观察,我正在读的关于javascript的书指出有一个带javascript的OOP?它没有说明什么,我的意思是它没有解释如何定义一个类。有人能给我一个样本片段吗?

    谢谢

    7 回复  |  直到 14 年前
        1
  •  1
  •   Daniel Vassallo    14 年前

    下面的代码片段可以帮助您开始使用JavaScript的无类、基于实例的对象:

    function getArea() {  
       return (this.radius * this.radius * 3.14);  
    }  
    
    function getCircumference() {  
       var diameter = this.radius * 2;  
       var circumference = diameter * 3.14;  
       return circumference;  
    }
    
    function Circle(radius) {  
       this.radius = radius;  
       this.getArea = getArea;  
       this.getCircumference = getCircumference;  
    }
    
    var bigCircle = new Circle(100);  
    var smallCircle = new Circle(2);
    
    alert(bigCircle.getArea());            // displays 31400  
    alert(bigCircle.getCircumference());   // displays 618  
    alert(smallCircle.getArea());          // displays 12.56  
    alert(smallCircle.getCircumference()); // displays 12.56
    

    例子来自: SitePoint - JavaScript Object-Oriented Programming

        2
  •  8
  •   rahul    14 年前

    JavaScript是 Prototype based 而不是基于阶级的。

    基于原型的编程是一种风格 面向对象编程 哪些课程不存在,以及 行为重用(称为继承 在基于类的语言中)执行 通过克隆现有的 用作原型的对象。这个 模型也可以称为无类模型, 面向原型或基于实例 编程。代表团是 支持的语言功能 基于原型的编程。

        3
  •  4
  •   Community Stefan Steinegger    7 年前

    我建议 This book for a简洁、准确地解释如何使用JS的原型继承以及如何在JS中模拟经典的OO继承。

    对于如何使用JS的原型继承以及如何在JS中模拟经典的OO继承的简明、精确的解释。

    JavaScript: The good parts

        4
  •  3
  •   Sam    14 年前

    javascript中的任何函数都可用于创建对象:

    例子:

    function MyPoint(x, y) {
        this.x = x;
        this.y = y;
        this.distanceTo = getDistance;
    }
    
    function getDistance(p) {
      var dx = this.x-p.x;
      var dy = this.y-p.y;
      return Math.sqrt(dx*dx + dy*dy);
    }
    
    var p0 = new MyPoint(1, 2);
    var p1 = new MyPoint(2, 3);
    
    window.alert('The distance is ' + p0.distanceTo(p1));
    
        5
  •  2
  •   Greg    14 年前

    这里有两种不同的方法

    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(); 
    

    另一种方式

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

    您还可以看到jquery、原型和类似的处理类的方式,看看是否适合您的需要。

        6
  •  2
  •   Community Stefan Steinegger    7 年前

    在JavaScript中没有一种标准的OOP方法。每个人都使用稍微不同的类/实例系统,大多数书籍都在回避这个问题。见 this question 讨论如何在JS中使用OO并选择您最喜欢的。

        7
  •  1
  •   Zimbabao    14 年前

    在javascript中,一切都是一个对象。所以即使是一个函数也是一个对象。因此,在JS(低于<版本2)中,函数生成类(这些类本身就是第一类对象)。开始 here , here here 为了更好地理解