I'm looking to prototype a method into a sub-object. Here is what I mean:

function object() {
    function subObject() {

    }
}

object.subObject.prototype.testMethod = function() {
    alert("test");
};

However, this seems to not work. Any idea on how to go about completing such a task?

有帮助吗?

解决方案

You need to create a property of the original function:

MyObject.SubObject = function(...) { ... };

Note that the two "classes" will not be related in any way; instances of either class will have no link to instances of the other class.
This is only useful for namespacing.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top