質問

Basically Dub and Dub.socialize objects already exist as an included library. I'm trying to extend the library with some additional custom functions that I created.

I attempted the following concept below:

Dub.socialize = {
    setUID : function(preUID, postUID)
    {
        // .. function code here
    }
}

However, I receive the following error "Uncaught TypeError: Cannot set property 'setUID' of undefined" from my console.

Obviously my knowledge of objects is a bit misled. What would be the proper method of extending this function into the already existing object library?

役に立ちましたか?

解決

A simple solution could be

Dub.socialize.setUID =  function(preUID, postUID) {};

他のヒント

Try this:

Dub.socialize.prototype.setUID = function(preUID, postUID) {
    ...
};

Object Constructor and prototyping

Edit: Realized you're working with a "static" object. This only works for something that is instantiated, and since you're not making new instances, this doesn't apply.

If you are going to create the function for declared object means then you have to use "prototype" keyword for example.

`var Dub = { socialize: new Object() };

Dub.socialize.prototype.setUID = function(preUID, postUID) { // Function Body };`

http://www.javascriptkit.com/javatutors/proto3.shtml

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top