mhulse / js-patterns

Some of my favorite JavaScript plugin design patterns: The Facade Pattern, The Revealing Module Pattern, Immediately-invoked Function Expressions (IIFE)s, The Module Pattern imports and exports …
3 stars 1 forks source link

Constructor pattern #10

Open mhulse opened 6 years ago

mhulse commented 6 years ago

Via @caseydedore:

var models = {
  Course: function () {
    this.id = 0;
    this.title = '';
    this.itemCode = '';
    this.type = 0;
    this.ccYear = 0;
    this.credits = 0;
    this.lessons = [];
    this.prerequisites = [];
    this.prerequisiteGroups = [];
    this.prerequisiteMaterials = [];
    this.prerequisiteOthers = [];
    this.comments = '';
    this.isCore = false;
    this.isPrint = false;
    this.getIsLocal = function () {
      return this.itemCode.indexOf('.LC') >= 0;
    };
    this.getIsApplication = function () {
      return this.type === 4;
    };
    this.isAdvanced = function () {
      return this.type !== 4 &&
        !this.getIsLocal() &&
        !this.isCore;
    };
    this.isPrint = function () {
      return this.type !== 2;
    };
    this.application = {
      id: '',
      title: '',
      number: ''
    };
  },
  Worksheet: function () {
    this.id = 0;
    this.comments = '';
    this.title = '';
    this.type = 'roadmap';
    this.author = '';
    this.tooltips = [];
    this.trackDefaults = '';
    this.ccID = '';
    this.comment = '';
    this.lastUpdated = '';
    this.createDate = '';
    this.programId = 0;
    this.curriculumStartDate = '';
    this.clcsSelectable = 0;
    this.programDefault = 0;
    this.sortOrder = 0;
    this.getWorksheetType = function() {
      if (uiConfig.adminMode.getIsAdmin()) {
        return $('input[type=radio][name=worksheet_type]:checked').val();
      } else if ($defaultViewSelector.find('option:selected').attr('data-snapshot') == '1' ||
        $('#radio_snapshot').prop('checked')) {
        return 'snapshot';
      }
      return this.type;
    };
  }
};
// var course = new models.Course();
// var worksheet = new models.Worksheet();

http://www.samselikoff.com/blog/some-Javascript-constructor-patterns/