0

I want to use item.create() in projectModule, testCase and businessFunction. because it's same prototype as item's I copied the item's to other functions. But i want have a one unique variable for these three functions which is called itemType. I tried this. But an exception is occurred. How can I place a unique variable for these three functions without overriding them?

Exception:

projectModule.create.prototype.itemType = 'module';
TypeError: Cannot read property 'prototype' of undefined 

JavaScript Code

 var item = function () {};
    item.prototype.create = function (projectName, itemName) {
      console.log(projectName);
      console.log(itemName);
      console.log(itemType);
    };


/**
 * create module
 */
var projectModule = function () {};
projectModule.prototype = Object.create(item.prototype);
projectModule.create.prototype.itemType = 'module';

/**
 * create tc
 */
var testCase = function () {};
testCase.prototype = Object.create(item.prototype);
testCase.create.prototype.itemType = 'tc';


/**
 * create bc
 */
var businessComponent = function () {};
businessComponent.prototype = Object.create(item.prototype);
businessComponent.create.prototype.itemType = 'bc';
s1n7ax
  • 2,750
  • 6
  • 24
  • 53
  • There should not be any `.create` in the `….prototype.itemType`? – Bergi May 21 '16 at 18:43
  • Also the way you wrote it, `.itemType` will be a property of the instance (`this`, [see here about promise callbacks](http://stackoverflow.com/q/20279484/1048572)) not a variable in the scope of your `create` method – Bergi May 21 '16 at 18:44

1 Answers1

0

This seems to have solved the error for me. Swapped .create and .prototype around.

var projectModule = function () {};

projectModule.prototype = Object.create(item.prototype);
projectModule.prototype.create.itemType = 'module';

/**
 * create tc
 */
var testCase = function () {};
testCase.prototype = Object.create(item.prototype);
testCase.prototype.create.itemType = 'tc';


/**
 * create bc
 */
var businessComponent = function () {};
businessComponent.prototype = Object.create(item.prototype);
businessComponent.prototype.create.itemType = 'bc';