Reputation: 1816
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"?
Upvotes: 0
Views: 56
Reputation: 490203
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() { } };
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
.
Upvotes: 1