javascript - javascript中正确的类构造

标签 javascript oop class

我正在学习使用 Javascript 的 OO 编程,似乎有很多方法可以创建一个类。我已经设法将我读到的大部分内容提炼成一个通用结构,但我觉得我可能没有理解原型(prototype)设计的目的是为了避免臃肿。在这个“类”中包含原型(prototype)定义是否会使它们变得毫无意义(即不减小对象大小)?这个整体架构有什么缺陷吗?

谢谢,

var Car = (function () {

    //Private fields
    var _make;
    var _model;

    //Constants (protected by encapsulation)    
    var NUM_OF_WHEELS = 4;

    //Private methods
    function getDesc() {
        return _make + " " + _model;
    }

    //Public constructor
    function thisCar(make, model, color) {
        _make = make;
        _model = model;

        //public properties
        thisCar.prototype.color = color;
    }

    //static properties
    thisCar.carsInTheWorld = 50;

    //static methods
    thisCar.getNumberOfWheels = function () {
        return NUM_OF_WHEELS * thisCar.carsInTheWorld;
    };

    //public properties
    thisCar.prototype.color = "";

    //public method
    thisCar.prototype.startEngine = function () {
        console.log(getDesc() + " engine started");
    };

    return thisCar;
})();

编辑 阅读评论和此处提供的链接后,我明白了。

需要访问私有(private)成员的类成员不能添加到原型(prototype)中并且必须具有特权。同样,通常的做法是不在构造函数中包含原型(prototype)。

var Car = function (make, model, color) {//Public constructor
    //Private fields
    var _make = make;
    var _model = model;

    //Public fields 
    this.color = color; 

    //Private methods
    function getDesc() {
        return _make + " " + _model;
    }

    //Privileged functions (requiring private members) 
    this.startEngine = function () {
        console.log(getDesc() + " engine started");
    };
};

//public method
Car.prototype.sprayPaint = function (newColor) {
    var oldColor = this.color;
    this.color = newColor;
    console.log("Your car has changed from " + oldColor + " to " + newColor);
};

//Public fields  (if not declared in constuctor)
Car.prototype.fuel = 0; 

//static properties
Car.carsInTheWorld = 0;

//static methods
Car.getNumberOfWheels = function () {               
    var NUM_OF_WHEELS = 4;
    return NUM_OF_WHEELS * Car.carsInTheWorld;
};

感谢您的所有回复,它们真的很有帮助。

最佳答案

不使用原型(prototype),一种常见的模式是从包含公共(public)访问器/修改器的构造函数返回一个对象:

jsfiddle

function Car() {
  var myPrivateVar = "foo";
  var myPublicVar = "bar";

  function myPrivateFunc() {console.log(myPrivateVar);};
  myPrivateFunc();      

  return {
    getMyPublicVar: function() {
      return myPublicVar;
    }
  }
}
var honda = Car();
console.log(honda.getMyPublicVar());
honda.myPrivateFunc();    // fail

在类定义之外使用原型(prototype)为其添加属性。您将使用 Car.prototype.myFunc = function() {}; after Car 已被定义以添加 myFunc 到将来实例化的 Car 对象。这些添加的成员是公开的。

Here's a question that should help you

Here's the standard article from Crockford on doing inheritance this way.

我应该重申,使用类框架,如 JS.Class当您的代码变大时,将为您省去无数的麻烦。

关于javascript - javascript中正确的类构造,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9984598/

相关文章:

javascript - 在我的代码中收到以下错误 "Invalid left-hand side in assignment"

java - 使用变量调用访问器

java - 创建具有不同属性的对象

c++ - 为什么指向对象的第二个指针未设置为 NULL 而是对象指针?

C++ 类成员和延迟初始化

javascript - 带有 flex 子项的 Flex 包裹

javascript - 在刷新时向多个元素添加相同的随机类?

javascript - Angular 2基于动态值而不是时间的动画过渡

css - Attributes.Add ("class", "className") 但保留现有类

c++ - 模板参数比较为假时调用的 if 语句