0

I'm working on trying to build a small library of functions within an object prototype. I'd perviously just been throwing my helper functions as global functions, however i'm trying to migrate to something more self contained.

Anyway my proto wrapper looks like this;

Q.fn = jHelper.prototype = {

    // helper functions
    addClass: function() {}, //...
    removeClass: function() {}, //...  
    // etc..

    // I want to add a child prototype method here that can be called
    Parent: function() {
        child: function(args) {
           console.log(args);
        }
    }

}

Q.Parent.child("test");

The problem is that I can't call functions inside "Parent". How do set this up so I can add child functions as a prototype of "Parent"?

1 Answer 1

1

Your Parent is pointing to a function, with has a label pointing to a function.

It would need to look like this...

Parent: {
    child: function(args) {
       console.log(args);
    }
}

This also assumes that Q.fn points to Q.prototype.


I want "child" to be a prototype method of "Parent"

You would need to set it up like a normal prototype.

You could (if your targets supported __proto__) set it up directly like so...

Parent.__proto__ = { child: function() { } };

jsFiddle.

This means that Parent's prototype chain (note: don't give it a capital letter as that's a convention for constructor functions) will look like this: Parent -> Object with child -> Object.prototype.

Object.prototype is the end of the line, you can see this by evaluating ({}).__proto__ === Object.prototype.

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

2 Comments

I want "child" to be a prototype method of "Parent";
This is what the full wrapper looks like, not sure if this is right way of going about it though; jsfiddle.net/b86roet2

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.