I have a complex Node SDK project that uses some class inheritance for try and javascript static-ify. I use the caching behavior of the Node module to create single-user behavior for the SDK (class Project, shared instance ProjectClient)). For initialization, it looks like this:
var Project = require('./project'),
Project.init(params)
I have classes of classes of objects for data: Entity(the standard parsed object is a JSON payload) and User(a special type of Entity that contains user properties).
The class ProjectClienthas several methods that allow you to run RESTful API calls, for example. Project.GET(), Project.PUT(). They work great when creating a Project"singleton".
Now I'm trying to create convenience-related methods Entitythat will use ProjectClientRESTful operations , for example. Entity.save(), Entity.refresh().
When I try to import Projectinto Entity:
var Project = require('../project')
I get:
TypeError: The super constructor to `inherits` must have a prototype.
at Object.exports.inherits (util.js:756:11)
Troubleshooting leads me to what this is related to util.inherits(ProjectUser, ProjectEntity)in User, because if I comment on it, I get this instead:
Uncaught TypeError: ProjectEntity is not a function
inherits? , Entity ? , , (, ), - , :
module.exports = _.assign(module.exports, **ClassNameHere**)
:
Entity
var Project = require('../Project'),
_ = require('lodash')
var ProjectEntity = function(obj) {
var self = this
_.assign(self, obj)
Object.defineProperty(self, 'isUser', {
get: function() {
return (self.type.toLowerCase() === 'user')
}
})
return self
}
module.exports = ProjectEntity
( Entity)
var ProjectEntity = require('./entity'),
util = require('util'),
_ = require('lodash')
var ProjectUser = function(obj) {
if (!ok(obj).has('email') && !ok(obj).has('username')) {
throw new Error('"email" or "username" property is required when initializing a ProjectUser object')
}
var self = this
_.assign(self, ProjectEntity.call(self, obj))
return self
}
util.inherits(ProjectUser, ProjectEntity)
module.exports = ProjectUser
( "singleton", )
'use strict'
var ProjectClient = require('./lib/client')
var Project = {
init: function(options) {
var self = this
if (self.isInitialized) {
return self
}
Object.setPrototypeOf(Project, new ProjectClient(options))
ProjectClient.call(self)
self.isInitialized = true
}
}
module.exports = Project
Client
var ProjectUser = require('./user'),
_ = require('lodash')
var ProjectClient = function(options) {
var self = this
return self
}
ProjectClient.prototype = {
GET: function() {
return function() {
}
},
PUT: function() {
return function() {
}
}
}
module.exports = ProjectClient