Skip to main content
deleted 48 characters in body
Source Link

Template for LampPost objects. Create instances with LampPostTemplate.create().

// Helper function to create copies of a template object and then copy
// arbitrary properties based on an input dictionary.
function createInstanceFromTemplate(templateObject, instanceVariableDictionary) {
  var result = Object.create(templateObject);
  for (var property in instanceVariableDictionary) {
    if (typeof instanceVariableDictionary[property]['slice'] === 'function') {
      // Copy an array
      result[property] = instanceVariableDictionary[property].slice();
    } else if (typeof instanceVariableDictionary[property] === 'object') {
      // Copy an object
      result[property] = Object.create(instanceVariableDictionary[property]);
    } else {
      // Copy numbers, strings, ...
      result[property] = instanceVariableDictionary[property];
    }
  }
  return result;
};

// Template for LampPost objects. Create instances with LampPostTemplate.create().
LampPostTemplate = {
  create:function(instanceVariableDictionary) {
    var result = createInstanceFromTemplatecreateInstance(this, instanceVariableDictionary);
    //... any initialization code needed for LampPost instances.
    result.flickerAnimationTimeOffset = Math.random();
    return result;
  },
  draw:function() { /* ... */ },

  center:{x:.5, y:.5},
  position:{x:0, y:0},
  name:"Unnamed",
  flickerAnimation: [1,1,1,1,1,0,1,1,0,1,1,1,0],
  flickerAnimationTimeOffset: 0
};

// ...

// Create instances:

Create instances:

Lamp1 = LampPostTemplate.create({ position:{x:1, y:2} });
Lamp2 = LampPostTemplate.create({
  position:{x:11, y:22},
  flickerAnimation:[1,0,0,0,0,1,0,0],
  name:"Hill top2"});

Here is the helper function to create copies of a template object and then copy arbitrary properties based on an input dictionary.

function createInstance(templateObject, instanceVariableDictionary) {
  var result = Object.create(templateObject);
  for (var property in instanceVariableDictionary) {
    if (typeof instanceVariableDictionary[property]['slice'] === 'function') {
      // Copy an array
      result[property] = instanceVariableDictionary[property].slice();
    } else if (typeof instanceVariableDictionary[property] === 'object') {
      // Copy an object
      result[property] = Object.create(instanceVariableDictionary[property]);
    } else {
      // Copy numbers, strings, ...
      result[property] = instanceVariableDictionary[property];
    }
  }
  return result;
};
// Helper function to create copies of a template object and then copy
// arbitrary properties based on an input dictionary.
function createInstanceFromTemplate(templateObject, instanceVariableDictionary) {
  var result = Object.create(templateObject);
  for (var property in instanceVariableDictionary) {
    if (typeof instanceVariableDictionary[property]['slice'] === 'function') {
      // Copy an array
      result[property] = instanceVariableDictionary[property].slice();
    } else if (typeof instanceVariableDictionary[property] === 'object') {
      // Copy an object
      result[property] = Object.create(instanceVariableDictionary[property]);
    } else {
      // Copy numbers, strings, ...
      result[property] = instanceVariableDictionary[property];
    }
  }
  return result;
};

// Template for LampPost objects. Create instances with LampPostTemplate.create().
LampPostTemplate = {
  create:function(instanceVariableDictionary) {
    var result = createInstanceFromTemplate(this, instanceVariableDictionary);
    //... any initialization code needed for LampPost instances.
    result.flickerAnimationTimeOffset = Math.random();
    return result;
  },
  draw:function() { /* ... */ },

  center:{x:.5, y:.5},
  position:{x:0, y:0},
  name:"Unnamed",
  flickerAnimation: [1,1,1,1,1,0,1,1,0,1,1,1,0],
  flickerAnimationTimeOffset: 0
};

// ...

// Create instances:
Lamp1 = LampPostTemplate.create({ position:{x:1, y:2} });
Lamp2 = LampPostTemplate.create({
  position:{x:11, y:22},
  flickerAnimation:[1,0,0,0,0,1,0,0],
  name:"Hill top2"});

Template for LampPost objects. Create instances with LampPostTemplate.create().

LampPostTemplate = {
  create:function(instanceVariableDictionary) {
    var result = createInstance(this, instanceVariableDictionary);
    //... any initialization code needed for LampPost instances.
    result.flickerAnimationTimeOffset = Math.random();
    return result;
  },
  draw:function() { /* ... */ },

  center:{x:.5, y:.5},
  position:{x:0, y:0},
  name:"Unnamed",
  flickerAnimation: [1,1,1,1,1,0,1,1,0,1,1,1,0],
  flickerAnimationTimeOffset: 0
};

Create instances:

Lamp1 = LampPostTemplate.create({ position:{x:1, y:2} });
Lamp2 = LampPostTemplate.create({
  position:{x:11, y:22},
  flickerAnimation:[1,0,0,0,0,1,0,0],
  name:"Hill top2"});

Here is the helper function to create copies of a template object and then copy arbitrary properties based on an input dictionary.

function createInstance(templateObject, instanceVariableDictionary) {
  var result = Object.create(templateObject);
  for (var property in instanceVariableDictionary) {
    if (typeof instanceVariableDictionary[property]['slice'] === 'function') {
      // Copy an array
      result[property] = instanceVariableDictionary[property].slice();
    } else if (typeof instanceVariableDictionary[property] === 'object') {
      // Copy an object
      result[property] = Object.create(instanceVariableDictionary[property]);
    } else {
      // Copy numbers, strings, ...
      result[property] = instanceVariableDictionary[property];
    }
  }
  return result;
};
added 1538 characters in body
Source Link

Create a template object with all shared functions and member data. Then, use a create function to manufacture instances.

In the example below, you can provide arbitrary properties to be specified uniquely per instance, either overriding template parameters or adding new parameters unique to an instance.

// Helper function to create copies of a template object and then copy
// arbitrary properties based on an input dictionary.
function createInstanceFromTemplate(templateObject, instanceVariableDictionary) {
  var result = Object.create(templateObject);
  for (var property in instanceVariableDictionary) {
    if (typeof instanceVariableDictionary[property]['slice'] === 'function') {
      // Copy an array
      result[property] = instanceVariableDictionary[property].slice();
    } else if (typeof instanceVariableDictionary[property] === 'object') {
      // Copy an object
      result[property] = Object.create(instanceVariableDictionary[property]);
    } else {
      // Copy numbers, strings, ...
      result[property] = instanceVariableDictionary[property];
    }
  }
  return result;
};

// Template for LampPost objects. Create instances with LampPostTemplate.create().
LampPostTemplate = {
  create:function(instanceVariableDictionary) {
    var result = createInstanceFromTemplate(this, instanceVariableDictionary);
    //... any initialization code needed for LampPost instances.
    result.flickerAnimationTimeOffset = Math.random();
    return result;
  },
  draw:function() { /* ... */ },

  center:{x:.5, y:.5},
  position:{x:0, y:0},
  drawname:function()"Unnamed",
 { flickerAnimation: [1,1,1,1,1,0,1,1,0,1,1,1,0],
  flickerAnimationTimeOffset: 0
};

/*/ ... */ }
}
// Create instances:
Lamp1 = ObjectLampPostTemplate.create(LampPostTemplate{ position:{x:1, y:2} });
Lamp1.position.xLamp2 = 34;
Lamp1LampPostTemplate.create({
  position.:{x:11, y:22},
 = 52;flickerAnimation:[1,0,0,0,0,1,0,0],
  name:"Hill top2"});
LampPostTemplate = { 
  center:{x:.5, y:.5},
  position:{x:0, y:0},
  draw:function() { /* ... */ }
}

Lamp1 = Object.create(LampPostTemplate);
Lamp1.position.x = 34;
Lamp1.position.y = 52;

Create a template object with all shared functions and member data. Then, use a create function to manufacture instances.

In the example below, you can provide arbitrary properties to be specified uniquely per instance, either overriding template parameters or adding new parameters unique to an instance.

// Helper function to create copies of a template object and then copy
// arbitrary properties based on an input dictionary.
function createInstanceFromTemplate(templateObject, instanceVariableDictionary) {
  var result = Object.create(templateObject);
  for (var property in instanceVariableDictionary) {
    if (typeof instanceVariableDictionary[property]['slice'] === 'function') {
      // Copy an array
      result[property] = instanceVariableDictionary[property].slice();
    } else if (typeof instanceVariableDictionary[property] === 'object') {
      // Copy an object
      result[property] = Object.create(instanceVariableDictionary[property]);
    } else {
      // Copy numbers, strings, ...
      result[property] = instanceVariableDictionary[property];
    }
  }
  return result;
};

// Template for LampPost objects. Create instances with LampPostTemplate.create().
LampPostTemplate = {
  create:function(instanceVariableDictionary) {
    var result = createInstanceFromTemplate(this, instanceVariableDictionary);
    //... any initialization code needed for LampPost instances.
    result.flickerAnimationTimeOffset = Math.random();
    return result;
  },
  draw:function() { /* ... */ },

  center:{x:.5, y:.5},
  position:{x:0, y:0},
  name:"Unnamed",
  flickerAnimation: [1,1,1,1,1,0,1,1,0,1,1,1,0],
  flickerAnimationTimeOffset: 0
};

// ...

// Create instances:
Lamp1 = LampPostTemplate.create({ position:{x:1, y:2} });
Lamp2 = LampPostTemplate.create({
  position:{x:11, y:22},
  flickerAnimation:[1,0,0,0,0,1,0,0],
  name:"Hill top2"});
Source Link

LampPostTemplate = { 
  center:{x:.5, y:.5},
  position:{x:0, y:0},
  draw:function() { /* ... */ }
}

Lamp1 = Object.create(LampPostTemplate);
Lamp1.position.x = 34;
Lamp1.position.y = 52;