0

I have a file that looks like this:

function UserController(){};

UserController.prototype = {
    theData: [],
    findAll: function(callback) {
        callback( null, this.theData );
     },
    findByID: function(id, callback) {
        var result = null;
        var count = theData.length;
        for (var i = 0; i < count; i++) {
            if (this.theData[i]._id == id) {
                result = this.theData[i];
                break;
            }
        }

        callback(null, result);
    },
    findByName: function(name, callback) {
        var result = null;
        var count = theData.length;

        for (var i = 0; i < count; i++) {
            if (this.theData[i].name == name) {
                result = this.theData[i];
                break;
            }
        };

        callback(null, result);
    },
    save: function(users, callback) {
        var user = null;

        if(typeof(users.length) === 'undefined') {
            users = [users];
        }

        var count = users.length;
        for (var i = 0; i < count; i++) {
            user = users[i];
            user._id = userCounter++;
            user.created_at = new Date();
        };

        callback(null, users);
    }
};

When I inspect the object (based off this), it tells me that it is function UserController(){} and has no methods or properties.

I've never used prototyping in Javascript and I can't see a thing wrong with what I've done here. It should be noted that I'm using this with Node.js and Expressjs but I don't believe that would be causing this problem.

EDIT
For brevity's sake I only posted what I was currently using, but I have also had the exact same problem using the file like this:

var userCounter = 1;

function UserController(){};

UserController.prototype.theData = [];

UserController.prototype.findAll = function(callback) {
    callback( null, this.theData );
};

UserController.prototype.findByID = function(id, callback) {
    var result = null;
    var count = theData.length;

    for (var i = 0; i < count; i++) {
        if (this.theData[i]._id == id)
        {
            result = this.theData[i];
            break;
        }
    };

    callback(null, result);
};

UserController.prototype.findByName = function(name, callback) {
    var result = null;
    var count = theData.length;

    for (var i = 0; i < count; i++) {
        if (this.theData[i].name == name) {
            result = this.theData[i];
            break;
        }
    };

    callback(null, result);
};

UserController.prototype.save = function(users, callback) {
    var user = null;

    if(typeof(users.length) === 'undefined') {
        users = [users];
    }

    var count = users.length;
    for (var i = 0; i < count; i++) {
        user = users[i];
        user._id = userCounter++;
        user.created_at = new Date();
    };

    callback(null, users);
};

One more edit
If I put this in the same file and Inspect it I get the results I would expect:

function Test(){};

Test.prototype = {
  theData: [],
  hello: function(){
    return "Hello World!";
  }
};

I don't see the difference and I don't see an error in my UserController code.

12
  • I wonder if your problem is that your first part is a function, then you are adding functions in a way that I usually do for adding functions to objects. You may want to follow this and see if it helps: mckoss.com/jscript/object.htm Commented Jul 10, 2011 at 23:34
  • 1
    Have you actually run the code and tried to call methods? Don't believe every output. Commented Jul 10, 2011 at 23:35
  • @James Black: That is normal prototype inheritance. There is actually nothing wrong with the code. The prototope of a function is actually an object. Commented Jul 10, 2011 at 23:35
  • @James Black: I'm updating the question in a minute to address what you said. @Felix: Yes I have run the code and it says there is no method 'findByName'. Commented Jul 10, 2011 at 23:36
  • 1
    @JCOC611: That will achieve something different and is not related to prototypal inheritance anymore... Commented Jul 10, 2011 at 23:40

2 Answers 2

1

This link helped me figure it out.

Turns out it WAS a problem with Expressjs. When using another file, you have to specifically call export on methods and objects you want to use with require. Or alternately, you can just call this.blahblah = function(){}; instead of export. Then when you require a file, it automatically turns it into an object.

EDIT
I found a different solution. It was as simple as changing my require call to look like this: var userController = new (require('./controllers/user').UserController);
After that, I can use Prototyped functions and objects just fine which makes the file much more readable and easier to understand.

Sign up to request clarification or add additional context in comments.

Comments

0

Yes, UserController is a function. Does it have properties? Yes, it has two properties, namely length and prototype. All functions in JavaScript have these two properties. Why don't you see these properties? Because they are not enumerable properties. Properties in JavaScript can have one or more attributes. In ES3 they are ReadOnly, DontEnum, and DontDelete. In ES5 they are configurable, writable, enumerable, get, set, and value.

In either version, length and prototype are not enumerable, so your inspector will not show them to you.

Incidentally all the "methods" you wrote are properties of the object UserController.prototype, not UserController.

2 Comments

Thanks for the answer but the Inspect code I used actually takes prototype into effect I believe (or at least I know that it output what I expected on my "Test()" object.
So does your inspector say that the object Test has two properties or zero?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.