tfont
tfont

Reputation: 11233

extending an object with new functions in Javascript

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?

Upvotes: 0

Views: 107

Answers (3)

Dissident Rage
Dissident Rage

Reputation: 2716

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.

Upvotes: 1

kirubakar
kirubakar

Reputation: 199

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

Upvotes: 0

Rajesh Dhiman
Rajesh Dhiman

Reputation: 1896

A simple solution could be

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

Upvotes: 1

Related Questions