简体   繁体   中英

Prototype a method into a sub-object?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM